您好我有一张表tbl_relations
,看起来像
-----------------------------------
| id | source_id | target_id |
-----------------------------------
| 2 | 2 | 4 |
-----------------------------------
| 3 | 5 | 7 |
-----------------------------------
| 4 | 7 | 4 |
-----------------------------------
其他表tbl_looksup
看起来像
------------------------------
| id | language | value |
------------------------------
| 1 | 1 | abc |
------------------------------
| 1 | 2 | abc |
------------------------------
| 2 | 1 | abc |
-------------------------------
| 2 | 2 | abc |
-------------------------------
| 5 | 1 | abc |
-------------------------------
| 5 | 2 | abc |
-------------------------------
| 7 | 1 | abc |
-------------------------------
| 7 | 1 | abc |
-------------------------------
tbl_relations
以tbl_looksup
和tbl_relations.source_id
为tbl_relations.target_id
id of tbl_looksup
我的问题
我需要在tbl_relations
source_id
target_id
中找不到tbl_looksup
或id
中的那些记录。这意味着tbl_looksup
中不存在target_id = 4
。更详细地说,tbl_relations的第一条记录tbl_looksup
SELECT
tbl_relations.source_id,
tbl_relations.target_id,
tbl_relations.id,
tbl_looksup.`id` AS tblid
FROM
tbl_relations
LEFT JOIN tbl_looksup
ON tbl_relations.`source_id` != tbl_looksup.`id`
OR tbl_relations.`target_id` != tbl_looksup.`id`
GROUP BY tbl_relations.id
中不存在{{1}}。这是错误的记录。我需要找出这些记录。
到目前为止我做了什么
{{1}}
答案 0 :(得分:2)
为了获得所需的结果,您需要加入tbl_looksup
两次,因为有两列依赖于该表。
SELECT DISTINCT a.*
FROM tbl_relations a
LEFT JOIN tbl_looksup b
ON a.source_id = b.id
LEFT JOIN tbl_looksup c
ON a.target_id = c.id
WHERE b.id IS NULL OR
c.id IS NULL
要进一步了解联接,请访问以下链接:
输出
╔════╦═══════════╦═══════════╗
║ ID ║ SOURCE_ID ║ TARGET_ID ║
╠════╬═══════════╬═══════════╣
║ 2 ║ 2 ║ 4 ║
║ 4 ║ 7 ║ 4 ║
╚════╩═══════════╩═══════════╝
答案 1 :(得分:0)
SELECT
tbl_relations.source_id,
tbl_relations.target_id,
tbl_relations.id
FROM
tbl_relations
WHERE tbl_relations.source_id not in (select id from tbl_looksup)
OR tbl_relations.target_id not in (select id from tbl_looksup)
答案 2 :(得分:0)
尝试添加此内容:
WHERE tbl_relations。
target_id
IS NULL
答案 3 :(得分:0)
SELECT tbl_relations.id FROM tbl_relations
LEFT JOIN tbl_looksup
ON tbl_looksup.id = tbl_relations.source_id OR tbl_looksup.id = tbl_relations.target_id
WHERE tbl_looksup.id IS NULL