table1
id int(10)
fid int(10)
table2
id int(10)
visible int(10)
data varchar(100)Basically I want to select all the rows of table1, simple so far.SELECT * FROM `table1`Now for each row in table1 there MIGHT be a row in table2, if I do a "JOIN" then if there was NOT row in table2 for table1 then that row in table1 would be excluded, so we use "LEFT JOIN"
SELECT
*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.id
Result:
Perfect so far, but now I want to make sure it's visible, this is where the issue comes in. Since we see that the first row `visible`=1 and the second one it's null, and we want the same results we'd simple do something like:
SELECT
*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.id
WHERE
visible = 1 OR visible = NULLBut this only shows the first row... I could also do something like: SELECT
*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.id
WHERE
visible != 2Since none are equal to 2 this 'should' work but it still only shows the first row.If I try to do something inside of the SELECT clause it say I can't even find the row.
Example:
SELECT
IF(t2.visible >= 1,t2.visible,-1) AS visi, t1.*, t2.*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.idand SELECT
COALESCE(t2.visible, -1) AS visi, t1.*, t2.*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.idBoth give me:
So now I should be able to filter by it but when I try this:
SELECT
IF(t2.visible >= 1,t2.visible,-1) AS visi, t1.*, t2.*
FROM
`table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON t1.fid=t2.id
WHERE
visi = -1I get this:#1054 - Unknown column 'visi' in 'where clause'
Remember, all I want to do is select all the rows in table1, then if they exist append them to the end, but also make sure they are visible.
I believe in the real database tables visible is actually the user id and I need to make sure the correct user is accessing the data... after I access this data I will know if I should "update" or "insert" into "table2"


Sign In
Create Account



Back to top









