I have a table structure like the following:
table a:
id int
name varchar
table b:
id int
name varchar
table c:
id int
valid tinyint
table_a int
table_b int
Basically, table c maps between table a and b and also has a column to determine if something is valid (either a 1 or 0). I want to be able to select every name (only once) from table a and the linked ‘valid’ column from table c (and default to 0 if it doesn’t exist) and limit it by matching table_b to an id. Hopefully that makes sense.
Here is some example data to help show what I mean:
Using the table structure as above I have this data:
Table a:
(id, name)
1, row 1
2, row 2
3, row 3
Table b:
(id, name)
1, row a
2, row b
Table c:
(id, valid, table_a, table_b)
1, 1, 1, 1
Now, what I want to select is all of the names from table a, the ‘valid’ column from c (default to 0 if no row that matches) and limit the ‘valid’ fields by only those that table_b = 1. Hopefully that makes more sense?
So, I want back the following where b.id = 1:
(a.name, c.valid)
row 1, 1
row 2, 0
row 3, 0
Let me see if I got it. Are you looking for something like this?
This query:
Results in this:
If you want all
b.id = 1then you WILL NOT have ALL rows from table_a.