Answer the question
In order to leave comments, you need to log in
How to issue such sql-query?
There are two tables:
main :
_____________________
| PROD | LIST_ID |
-----------------------
| a | 3 |
-----------------------
| a | 4 |
-----------------------
| b | 1 |
-----------------------
list :
_____________________
| ID | LIST_VAL |
-----------------------
| 1 | 11 |
-----------------------
| 2 | 22 |
-----------------------
| 3 | 33 |
-----------------------
| 4 | 44 |
------------------------
You need to take all the lines from list and join them with each line from main (on the fields main.LIST_ID and list.ID ) and get the following:
_________________________________
| PROD | LIST_ID | LIST_VAL |
--------------------------------------
| a | 1 | null |
--------------------------------------
| a | 2 | null |
--------------------------------------
| a | 3 | 33 |
--------------------------------------
| a | 4 | 44 |
----------------------------------------------------
| b | 1 | 11 |
--------------------------------------
| b | 2 | null |
--------------------------------------
| b | 3 | null |
--------------------------------------
| b | 4 | null |
--------------------------------------
Answer the question
In order to leave comments, you need to log in
select l.prod, l.id list_id, l.list_val from list l left join main m on <Дальше сам>
SELECT `p`.`PROD` AS `PROD`, `l`.`ID` AS `LIST_ID`,
IF(`m`.`PROD` IS NULL, NULL, `l`.`LIST_VAL`) AS `LIST_VAL`
FROM (
SELECT DISTINCT `PROD`
FROM `main`
) AS `p`
JOIN `list` AS `l`
LEFT JOIN `main` AS `m`
ON `m`.`PROD` = `p`.`PROD` AND `m`.`LIST_ID` = `l`.`ID`
ORDER BY `p`.`PROD`, `l`.`ID`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question