我尝试查询表log
中的数据,但不包括表userlog
中的数据:
SELECT * FROM (`log`) LEFT JOIN `userlog` ON `userlog`.`userlogIdLog` = `log`.`LogId`
WHERE (logTypeCategory = 0 AND LogType = 1) OR (logTypeCategory = 19 AND LogType = 1)
OR (logTypeCategory = 0 AND LogType = 4) OR (logTypeCategory = 0 AND LogType = 8)
OR (logTypeCategory = 0 AND LogType = 12) OR (logTypeCategory = 2 AND LogType = 3)
OR (logTypeCategory = 0 AND LogType = 7) OR (logTypeCategory = 459 AND LogType = 9)
OR (logTypeCategory = 456 AND LogType = 3)
AND `userlogStatus` is null or userlogStatus <> 1
AND `userlog`.`userlogIdUser` = '458' ORDER BY `LogId` DESC
但此查询显示log
中的行,但在表userlog
中存在以下行:userlogIdLog = 1 AND userlogStatus = 1 AND userlogIdUser = 458
我在你的帮助下编辑:
select * from `log` as a
where not exists ( select 1 from `userlog` as b where b.`userlogIdLog` = a.`LogId` AND b.`userlogIdUser` = 458 )
AND (logTypeCategory = 0 AND LogType = 1)
OR (logTypeCategory = 19 AND LogType = 1)
OR (logTypeCategory = 0 AND LogType = 4)
OR (logTypeCategory = 0 AND LogType = 8)
OR (logTypeCategory = 0 AND LogType = 12)
OR (logTypeCategory = 2 AND LogType = 3)
OR (logTypeCategory = 0 AND LogType = 7)
OR (logTypeCategory = 459 AND LogType = 9)
OR (logTypeCategory = 456 AND LogType = 3)
但这不正确
答案 0 :(得分:3)
有几种方法可以从一个表格中选择数据,而这些数据不在另一个表格中:
e.g。
select *
from `log`
where `LogId` not in
(
select `userlogIdLog`
from `userlog`
)
或
select *
from `log` as a
where not exists
(
select 1
from `userlog` as b
where b.`userlogIdLog` = a.`LogId`
)
...然后在您选择上述陈述后,添加其余的逻辑;即。
AND
(
(logTypeCategory = 0 AND LogType = 1)
OR (logTypeCategory = 19 AND LogType = 1)
OR (logTypeCategory = 0 AND LogType = 4)
OR (logTypeCategory = 0 AND LogType = 8)
OR (logTypeCategory = 0 AND LogType = 12)
OR (logTypeCategory = 2 AND LogType = 3)
OR (logTypeCategory = 0 AND LogType = 7)
OR (logTypeCategory = 459 AND LogType = 9)
OR (logTypeCategory = 456 AND LogType = 3)
)
答案 1 :(得分:1)
select L.col1,L.col2,L.col3 From Log L Left Outer Join userlog UL on L.LogId = UL.userlogIdLog where UL.userlogIdLog is NULL