Reputation: 2105
Here is problem I have:
Table A id_a | name --------------------- 1 | name-A 2 | name-B 3 | name-C 4 | name-D 5 | name-E 6 | spcial_type
Table B id_b | id_a | condition | subtype ------------------------------------------------- 1 | 2 | 1 | 1 2 | 1 | 0 | 1 3 | 1 | 1 | 2 4 | 2 | 0 | 1 5 | 4 | 0 | 1 6 | 5 | 1 | 1 7 | 2 | 1 | 3
Terms:
Table A: “special_type” exluded
Table A: rows not present in Table B included
Table B: all with condition=0 exluded
Result Table: id_r | id_a | id_b | name | condition ----------------------------------------------- 1 | 1 | 3 | name-A | 1 2 | 2 | 1 | name-B | 1 3 | 5 | 6 | name-E | 1 4 | 2 | 7 | name-B | 1 5 | 3 | null | name-C | null
table A.subtype is only aux. to show that id_a can be stored many times with condition=1
What I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A LEFT JOIN Table z ON x. id_a = z. id_a
but this got me items with condition=0, which I do not want
so I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A LEFT JOIN Table z ON x. id_a = z. id_a where z.condition=1
but that idea excluded items from Table A not present in Table B, and I want these items.
Can it be don inside of MySQL, or do I need scripting lang. to sort it out?
Thoughts anyone?
Upvotes: 1
Views: 3068
Reputation: 2105
OK
I must have had a temp. black out.
Here it is:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A LEFT JOIN Table z ON x. id_a = z. id_a AND z.condition=1
condition below
AND z.condition=1
was the key, when placed in join condition not in where clause
Upvotes: 4