我一直在尝试让我的数据库显示我的联系人。但其他用户可以授予人们访问其联系人的权限(这全部存储在 tblaccess 中)
我尝试使用这个sql
SELECT *
FROM tblcontacts, tblaccess
WHERE (
tblaccess.user =1
AND tblaccess.contact = contactID
)
OR tblcontacts.userid =1
但只有当 tblaccess
有一行时,它才会返回结果。
条件是 if tblcontacts.userid <> 1 然后检查 tblaccess.user = 1 是否
提前致谢
最佳答案
这是我理解你的问题的方式。您需要 userid = 1
的所有联系人,除非 tblaccess
中有记录。如果有这样的记录,那么user
列必须为1。
以下查询实现了这一点:
SELECT *
FROM tblcontacts c left outer join
tblaccess a
on a.contact = c.contactID
where c.userid = 1 and
(a.concatId is NULL or
a.user = 1
);
关于Mysqli - 与或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20584123/