Compare and insert from multiple tables? - php

There are 4 tables table1,table2,table3 and table4
table1 has got 60000 datas
table2 has got 85000 datas
table3 has got 78000 datas
table4 has got 68000 datas
indexes on all tables are same but one got less than or more than each other. for example name john is stored in all 4 tables.but mathew is stored in may be two tables but not in other two and ethan may be stored in 3 tables but not may be in 4th one.
upto say first 60k all index/names are same but after that it is irregular
so how can I merge all these table into 1 table?? all four tables got 2 columns each and first one is name and second is its details

There are probably more efficient ways of doing this, but this was the first thing that came to mind.
INSERT INTO table5
SELECT DISTINCT Table5Content.* FROM (
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL
SELECT * FROM table4
UNION ALL
SELECT * FROM table5
) as Table5Content

Related

Display tables data one after one in MySQL?

actually I have two tables ,i.e table 1 and table 2 . I need to display data from both tables . if I use
SELECT * FROM table 1 UNION SELECT * FROM table 2
Now I am getting data randomly from both tables
BUT I need first display table1 data after display table2 data
In practice, this will do what you want (assuming the tables have the same columns):
SELECT * FROM table1
UNION ALL
SELECT * FROM table2;
It is actually safer to use an explicit order by:
SELECT . . .
FROM (SELECT t1.*, 1 as which FROM table1 t1
UNION ALL
SELECT t2.*, 2 as which FROM table2 t2
) t
ORDER BY which;
SQL tables represent unordered sets. There is no ANSI requirement that UNION ALL return values from the first subquery before the second. In practice that works in MySQL.
One way could be adding a column in the both queries which would indicate the source from where it came.
SELECT
*
FROM
(
SELECT *,'T1' AS source FROM table1
UNION
SELECT *,'T2' FROM table2
) AS t
ORDER BY t.source ASC;
See Demo

get all record without join in sql

i have two tables table1 and table2, table1 in 10 filed available and tbale2 in 6 filed available.but no any relation between them.
i want to get all record from both table.
Use cross join
Select t.*,t1.* from table t cross join table1 t1
If you want all the records in the same table use the above query it will join and give m*n rows where m and n are number of rows in the tables
You can use union all if you want all the results added m+n number of results
Select * from table
Union all
Select * from table1
You need to specify the columns if you need specific columns from both tables. Or if you have different number of columns in the tables
If you have at least some common columns, you can union them together. For example:
Table1
Name Description Quantity Price
Table2
Name Description OrderDate Blah BlahBlah
You can do something like this:
SELECT Name, Description FROM Table1
UNION ALL
SELECT Name, Description FROM Table2
That would give you a result set with 2 columns (Name, Description, OrderDate) that is made up of rows from both Table1 and Table2

How to select * from multiple table

I have two tables, table1 and table2. table1 has 2 rows. table2 has 3 rows. so, totally table1 and table2 have 5 rows. I want to show the 5 rows by selecting table1 and table2 at a time. how todo? can you help me please. don't add any where clause.
depends on what the table structure is. if you have a PK > FK relationship you can join the tables like so
SELECT stuff
FROM table1 t1
JOIN table2 t2 ON t1.someID = t2.someID
if there is no correlation then you can use a UNION
SELECT stuff
FROM table1
UNION
SELECT stuff
FROM table2
One thing to note about using a UNION. the columns have to match so if you have the same type of data in both tables this works fine or else you will have to specify which columns to be selected out.

Fetch all rows data from second table using single join query

I have run into a 1-N situation. i.e 1 record in 1st table will have multiple records in second table, I just wanna fetch all data corresponding to the ID in 1st table using joins, is this possible.
table 1 : ID name
1 pradeep
table 2: ID table1_id orders
1 1 23
2 1 25
3 1 26
In a single query i should get all records. I don't wanna loop as i am doing it and its taking lot of time.
I want to display user html output in manner like this. So there lies the problem.
ID Name orders1 orders2 orders order4
SELECT
*
FROM
table2
LEFT JOIN
table1 ON
table1.ID = table2.table1_id
SELECT
t2.* /* fetch data from second table */
FROM
table1 t1
LEFT JOIN table2 t2 on t2.table1_id = t1.id
WHERE t1.id = <id>;
I wouldn't recomment it though. Why don't you just run it as 2 separate queries?

combining 2 tables in MySQL select statement

I know I can do joins but its not exactly what I want.
I'm making a live chat system that has 2 tables mainly: the main chat table (call it table a), and then a mod table (call this one table b). If a user gets suspended, messages reach over 100 for that channel, or they are over 1 week, the messages get moved from the main chat table to the mod table.
I store the ID of the chat messages as ID(primary) on the main chat table and as chatID on the mod table.
What I'm doing is making a separate page for my Mods and I want to be able to combine the two tables into 1 area but I want them to be ordered by their respective tables.
So lets say we had the following:
Main table ID's: 1,2,4
Mod table ID: 3
I want my results to show up 1,2,3,4 no matter which table the ID is in.
Any help is greatly appreciated!
Edit: I got the answer and this is what I used to do so:
SELECT ab.* FROM
((SELECT ID as table_id FROM a
WHERE roomID = 'newUsers' ORDER BY ID ASC)
UNION ALL
(SELECT chatID as table_id FROM b
WHERE roomID = 'newUsers' ORDER BY chatID ASC)) ab
ORDER BY ab.table_id
Use a UNION in a subselect.
SELECT ab.* FROM (
SELECT 1 as table_id, * FROM a
UNION ALL
SELECT 2 as table_id, * FROM b
) ab
ORDER BY ab.id
If you want the result of table A to appear before table B, change the query to:
SELECT ab.* FROM (
SELECT 1 as table_id, * FROM a
UNION ALL
SELECT 2 as table_id, * FROM b
) ab
ORDER BY ab.table_id, ab.id
Some background
UNION ALL will merge two tables resultsets into one resultset.
UNION will do the same but will eliminate duplicate rows.
This takes time and slows things down, so if you know there will be no duplicate records (or you don't care about dups) use UNION ALL.
See also: http://dev.mysql.com/doc/refman/5.5/en/union.html

Categories