Home > Blockchain >  Multiples SELECT in MySql
Multiples SELECT in MySql

Time:10-18

I try to multiple select in MySql but it doesn't work.

Example :

SELECT (SELECT column1, column2 FROM table1 WHERE column3='something') AS query1,
(SELECT column4 FROM table1 WHERE column4='something') AS query2

And the result i try to have is something similar to this :

[{query1}, {query2 : [...]}]

query1 always will have only 1 row but query2 can will have multiple row

Can someone help me please ?

CodePudding user response:

Maybe try this way: WITH query 1 AS (Select .....), query 2 AS (select ...), query3 AS ( SELECT * FROM query 1 UNION ALL SELECT * FROM query 2)

SELECT * FROM query3

CodePudding user response:

Is this what you mean ? Join column wise?

mysql> SELECT * FROM table1;
 --------- --------- --------- --------- 
| Column1 | Column2 | Column3 | Column4 |
 --------- --------- --------- --------- 
|       1 | XYZ     | ABC     |     255 |
|       2 | YZEX    | AVF     |     567 |
|       3 | AGH     | TYRE    |    5467 |
|       4 | ZXS     | KJA     |     456 |
|       5 | AAA     | FRES    |      56 |
 --------- --------- --------- --------- 

And CTE to combine columns:

with cte1 AS (SELECT column1, column2 FROM table1 WHERE column3='ABC'), 
cte2 AS (SELECT column4 FROM table1 WHERE column4=567)
SELECT * FROM cte1 JOIN cte2;

 --------- --------- --------- 
| column1 | column2 | column4 |
 --------- --------- --------- 
|       1 | XYZ     |     567 |
 --------- --------- --------- 
1 row in set (0.00 sec)
  • Related