Display tables data one after one in MySQL? - php

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

Related

How to use GROUP_CONCAT and UNION in single MySQL Statement

I have 2 tables in my database which are identical in structure but contain different records. Each table has the field 'keywords' which contains comma delimited data.
At the moment, I'm running 2 MySQL queries to get 2 different results which I then merge, using the following MySQL Statements:
SELECT GROUP_CONCAT(keywords) keywords FROM table1
and
SELECT GROUP_CONCAT(keywords) keywords FROM table2
I'm sure I should be able to get what I need using a single MySQL statement but all of my attempts to use GROUP_CONCAT and UNION have failed.
Thanks for any pointers/help.
Try this :
SELECT CONCAT(GROUP_CONCAT(t1.keywords),',',GROUP_CONCAT(t2.keywords)) keywords FROM table1 t1, table2 t2
If you don't want duplicate values, then it will be like that :
SELECT GROUP_CONCAT(DISTINCT t.keywords) FROM (
SELECT t1.keywords FROM table1 t1
UNION
SELECT t2.keywords FROM table1 t2
) t
It is as simple as that:
SELECT GROUP_CONCAT(keywords) keywords FROM table1
UNION
SELECT GROUP_CONCAT(keywords) keywords FROM table2
Here is a fiddle.

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.

SELECT Multiple Tables

I'm stuck in a piece of code that does not quite understand.
I have several tables with different names but same fields, but the tables are independent
Something like that:
table1
id
user
title
table2
id
user
title
I need to get in the same query data from two tables but I fail, I try with INNER JOIN, UNION ALL, but not knowing, it misapplied.
Right now I have this:
$mysites = $db->QueryFetchArrayAll("
select *
FROM table1,table2
where table1.user = table2.user AND
table1.user = 1");
foreach($mysites as $mysite){
echo $QUERY['title'];
}
but returned this:
title1.table1
title2.table1
and i like this:
title1.table1
title2.table1
title1.table2
title2.table2
A greeting and thanks
You can use the keyword UNION like this:
SELECT * FROM table1 UNION SELECT * FROM table2
This query will select everything from table1 and merge the results with those from table2. Please note that you have to select the same number of columns from both tables. Moreover, column names and datatypes will be assigned according to first table.
If you want to preserve duplicates add the keyword ALL:
SELECT * FROM table1 UNION ALL SELECT * FROM table2
The question is very unclear.....
Are the ID's the same in each table for each user? If so an INNERJOIN will help
SELECT t1.*, t2.*
FROM table1.t1
INNER JOIN table2.t2
ON t1.id = t2.id
WHERE t1.user = "1"
(Change INNER JOIN to LEFT JOIN if the data could be missing)
If this is not the case, why not put the data from one table into the other, and have just one table with all the data in it?

mysql two tables with the same structure - want to get one common field ordered by via a union

I have two tables that have the same structure, I need a query to order by from both tables at the same time so that the array fetched with php grabs the order correctly out of the two tables selected with a single query at the same time.
avoiding selection of each table separately, storing on php arrays and then sorting.
According to the documentation, it should be just like you would think. Here is an example from the documentation:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
select * from
(select * from tbl1
union all
select * from tbl2
) a
order by col1

Categories