简体   繁体   中英

MySql error while using CASE and Left Join

I have three tables table1, table2 and table 3. I am getting data to a file with sequence of the output as shown below. I want to append the data coming in that file to a table4 that ill create and persist using Java.

Scenario: I want all the data in this three tables into one cloumn where table2 element matches table3 element in C4 then instead of that matched element i want to replace that element with the corresponding data in C5 . For example in below mentioned table we see 11 in table2 matches table3 at column C4. Then the output should have 11_1, 11_2 instead of 11.

Help needed

Table1:                  Table2:                          Table3:

C1                       C3                               C4        C5
-----                    ------                           ------    -------
1                        11                               11        11_1
2                        12                               11        11_2
3                        13                               14        14_1
4                        14                               14        14_2
5                        15                               14        14_3
                         16                               null      null 

Code I worked:

SELECT c1 
FROM   (SELECT c1 
        FROM   table1 
        UNION ALL 
        SELECT c3 
        FROM   table2 
        UNION ALL 
        SELECT c5 
        FROM   table3) temp 

This query fetching me everything :(

SELECT C1 FROM(SELECT DISTINCT C1 AS C1 FROM table1 UNION ALL SELECT DISTINCT C2 FROM table2)AS 
temp LEFT JOIN table3 ON .temp.C1=table3.C4 CASE WHEN table3.C4=NULL THEN temp.C1 ELSE table3.C5

Output Required:

Table4
C1
-----
1
2
3
4
5
11_1
11_2
12
13
14_1
14_2
14_3
15
16

Help is appreciated

This should work:

SELECT col 
FROM   (SELECT c1 AS col 
        FROM   table1 
        UNION ALL 
        SELECT CASE 
                 WHEN t3.c4 IS NULL THEN t2.c3 
                 ELSE t3.c5 
               end AS col 
        FROM   table2 t2 
               LEFT JOIN table3 t3 
                      ON t3.c4 = t2.c3) temp 

Result

|  COL |
--------
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
| 11_1 |
| 11_2 |
|   12 |
|   13 |
| 14_1 |
| 14_2 |
| 14_3 |
|   15 |
|   16 |

See the demo

This will get everything from Table 3 that matched Table3.C4 to Table2.C1. It returns ROWS:

SELECT C5
FROM Table3
LEFT JOIN Table2 ON Table2.C3 = Table3.C4

I've only be here a short time and I consistently see questions where people want ROWS returned in COLUMNS. SQL returns matching ROWS, not COLUMNS. The SQL statement above will list the data as follows:

11_1
11_2
:
:

If you want the rws converted to columns, do it in the language of your choice. (Python makes this kind of thing nice! :) )

Matt

This will produce the specified output, BUT the order of rows is not deterministic:

SELECT t1.c1
  FROM table1 t1
 UNION ALL
SELECT IFNULL(t3.c5,t2.c3)
  FROM table2 t2
  LEFT
  JOIN table3 t3
    ON t3.c4 = t2.c3

To get the rows in a specified ordered, we need to do more work. If there are no "duplicates" returned by the previous query, and we aren't concerned that any duplicates might be removed:

SELECT t1.c1
  FROM table1 t1
 GROUP BY t1.c1
 UNION ALL
SELECT IFNULL(t3.c5,t2.c3)
  FROM table2 t2
  LEFT
  JOIN table3 t3
    ON t3.c4 = t2.c3
 GROUP
    BY t2.c3
     , t3.c5

SQL Fiddle demo

The intent is to populate another table with this resultset, that can be done with a SQL statement:

INSERT INTO table4 (c1)
SELECT t1.c1
  FROM ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM