SELECT binds the result of two queries to MYSQL

I want to link the contents of two queries. Please note that I do NOT want to use CONCAT() or CONCAT_WS() , because this is not what I want to achieve.

Query Results 1:

 Id Name 1 John 2 Mike 

Query Results 2:

 Id Name 3 Emily 7 Robert 

I want too:

 Id Name 1 John 2 Mike 3 Emily 7 Robert 

I can also rewrite both queries as one. That would be wonderful. Here are my current failed attempts:

 SELECT ax AS x, ay AS y FROM a; SELECT bx AS x, by AS y FROM b; 

Thanks.

+4
source share
5 answers

use UNION ALL to combine multiple queries into a single result set:

 SELECT ax AS x, ay AS y FROM a UNION ALL SELECT bx AS x, by AS y FROM b 

Note

UNION ALL saves all redundancies. read the manual page and modify the query to achieve the desired behavior.

+4
source
 SELECT ax AS x, ay AS y FROM a union all SELECT bx AS x, by AS y FROM b; 
+1
source

Use UNION :

 SELECT ax AS x, ay AS y FROM a UNION ALL SELECT bx AS x, by AS y FROM b; 
+1
source

How about using union?

 SELECT ax AS x, ay AS y FROM a UNION SELECT bx AS x, by AS y FROM b; 
+1
source

It seems you need a union:

 SELECT ax AS x, ay AS y FROM a UNION SELECT bx AS x, by AS y FROM b 

If you want to keep backups, use UNION ALL.

0
source

Source: https://habr.com/ru/post/1397008/


All Articles