我有这样的查询:
mysql> SELECT
-> ir.id, ir.no_surat, link.condition_id
-> FROM inspection_report ir
->
-> LEFT JOIN link_item_condition_ir_to_ir link
-> ON link.inspection_id = ir.id
->
-> WHERE ir.no_surat LIKE '%E67%'
-> ORDER BY ir.no_surat ASC, link.condition_id DESC;
+------+---------------+--------------+
| id | no_surat | condition_id |
+------+---------------+--------------+
| 7561 | E6779/10/2018 | 1 |
| 7562 | E6780/10/2018 | 5 |
| 7562 | E6780/10/2018 | 1 |
| 7563 | E6781/10/2018 | 5 |
| 7563 | E6781/10/2018 | 1 |
+------+---------------+--------------+
5 rows in set (0.03 sec)
请在no_surat
栏中查看。
有3个唯一的no_surat
。我的目标是:选择没有condition_id = 5
的数据。
我也已经尝试过,
mysql> SELECT
-> ir.id, ir.no_surat, link.condition_id
-> FROM inspection_report ir
->
-> LEFT JOIN link_item_condition_ir_to_ir link
-> ON link.inspection_id = ir.id
->
-> WHERE ir.no_surat LIKE '%E67%'
-> AND
-> condition_id != 5
->
-> ORDER BY ir.no_surat ASC, link.condition_id DESC;
+------+---------------+--------------+
| id | no_surat | condition_id |
+------+---------------+--------------+
| 7561 | E6779/10/2018 | 1 |
| 7562 | E6780/10/2018 | 1 |
| 7563 | E6781/10/2018 | 1 |
+------+---------------+--------------+
3 rows in set (0.02 sec)
但仍然无法正常工作, 请告知。
我期望的结果是,忽略所有具有condition_id = 5的'no_surat'列
+------+---------------+--------------+
| id | no_surat | condition_id |
+------+---------------+--------------+
| 7561 | E6779/10/2018 | 1 |
+------+---------------+--------------+
谢谢
该查询如何:
mysql> SELECT
-> ir.id, ir.no_surat, link.condition_id
-> FROM inspection_report ir
->
-> LEFT JOIN link_item_condition_ir_to_ir link
-> ON link.inspection_id = ir.id
->
-> WHERE ir.no_surat LIKE '%E67%'
-> GROUP BY ir.id
->
-> HAVING GROUP_CONCAT( link.condition_id ) NOT LIKE "%5%";
+------+---------------+--------------+
| id | no_surat | condition_id |
+------+---------------+--------------+
| 7561 | E6779/10/2018 | 1 |
+------+---------------+--------------+
1 row in set (0.00 sec)
看起来不错吗?
这是小提琴:sqlfiddle
答案 0 :(得分:2)
使用此条件:
NOT EXISTS(SELECT 1 FROM link_item_condition_ir_to_ir
WHERE inspection_id = ir.id AND condition_id = 5)
代替condition_id != 5
。
答案 1 :(得分:1)
SELECT ir.id, ir.no_surat, link.condition_id
FROM inspection_report ir
LEFT JOIN link_item_condition_ir_to_ir link
ON link.inspection_id = ir.id
WHERE ir.no_surat LIKE '%E67%' AND
ir.no_surat NOT IN
(SELECT ir.no_surat
FROM inspection_report ir
LEFT JOIN link_item_condition_ir_to_ir link
ON link.inspection_id = ir.id
WHERE condition_id = 5)
ORDER BY ir.no_surat ASC, link.condition_id DESC;
答案 2 :(得分:1)
请尝试在“查询”下面,我正在使用您的查询来获取结果。
npm install
如果您想更好地查询,请共享您的数据库结构(Schema):)
答案 3 :(得分:1)
使用“不存在”
SELECT t.* from
(select ir.id, ir.no_surat, link.condition_id
FROM inspection_report ir
LEFT JOIN link_item_condition_ir_to_ir link
ON link.inspection_id = ir.id
) t where
NOT EXISTS ( select 1 from link_item_condition_ir_to_ir
t1 where t1.inspection_id=t.id and t1.condition_id=5
)
http://www.sqlfiddle.com/#!9/647c0e/3
id no_surat condition_id
7561 E6779/10/2018 1