Search a value in several tables PHP + Mysql
I'm doing some maintenance on a system which has several tables for each access level.
I need to verify a field called IID in every table so I can see the person access level.
Who made the code used three separated searches:
$var->exeQuery("SELECT iid FROM leve11 WHERE iid=$iid");
$var->exeQuery("SELECT iid FROM leve13 WHERE iid=$iid");
$var->exeQuery("SELECT iid FROM leve12 WHERE iid=$iid");
I know that I can make it using a JOIN, but I'm doing something wrong because nothing is returned.
I'm开发者_如何学JAVA a noob! :)
SELECT iid FROM level1 WHERE iid=$iid
UNION ALL
SELECT iid FROM level2 WHERE iid=$iid
UNION ALL
SELECT iid FROM level3 WHERE iid=$iid
or if the info is different and you want to join instead of union....
SELECT level1.iid, level2.iid, level3.iid FROM level1
LEFT JOIN level2
ON level1.iid = level2.iid
LEFT JOIN level3
on level2.idd = level3.iid
WHERE iid=$iid
select
t.iid, l1.iid as Level1, l2.iid as Level2, l3.iid as Level3
from
master_table t
left join
level1 l1 on t.iid = l1.iid
left join
level2 l2 on t.iid = l2.iid
left join
level3 l3 on t.iid = l3.iid
where
t.iid = $iid
The user has any level that does not return Null
as the result.
How about something like this:
select iid from level1
inner join level2 on level1.iid = level2.iid
inner join level3 on level3.iid = level2.iid
where level1.iid = $iid
精彩评论