我正在对每个访问级别都有几个表的系统进行一些维护。
我需要在每个表中验证一个名为IID的字段,以便我可以看到人员访问级别。
谁使代码使用了三个单独的搜索:
$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");
我知道我可以使用JOIN来制作它,但我做错了,因为没有返回。
我是菜鸟! :)
答案 0 :(得分:1)
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
或者如果信息不同而你想加入而不是工会......
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
答案 1 :(得分:0)
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
用户的任何级别都不会返回Null
作为结果。
答案 2 :(得分:0)
这样的事情怎么样:
select iid from level1
inner join level2 on level1.iid = level2.iid
inner join level3 on level3.iid = level2.iid
where level1.iid = $iid