[英]MySQL select all left entries from a table which is joined from another table
I have the following MySQL-Statement: 我有以下MySQL声明:
SELECT norm.NormID, norm.NormName
FROM (assignment
INNER JOIN norm
ON assignment.NID = norm.NormID )
INNER JOIN wire
ON assignment.LID = wire.WireID
WHERE wire.WireID= 109
ORDER BY norm.NormName;
Now what I got are the entries from the table assignment with the NormID and NormName for that WireID. 现在,我得到的是表分配中带有该WireID的NormID和NormName的条目。 What I want to get are the entries from the table norm, which are not setted for this WireID.
我要获取的是表规范中的条目,这些条目未为此WireID设置。
Eg: WireID has the norm assignment A, B, D, G. 例如:WireID具有规范分配A,B,D,G。
The table norm has the entries A, B, C, D, E, F, G, H. 表格规范具有条目A,B,C,D,E,F,G,H。
What I want to get from the MySQL-Statment are the entries C, E, F, H. 我想从MySQL语句中获得的是条目C,E,F,H。
How can I select those left norm entries for this WireID? 如何为该WireID选择那些剩余的规范条目?
With the above statement I would get: 有了以上声明,我将得到:
-----------------------
| NormID | NormName |
-----------------------
| 1 | A |
| 2 | B |
| 4 | D |
| 7 | G |
-----------------------
I want to have this Table: 我想要这个表:
-----------------------
| NormID | NormName |
-----------------------
| 3 | C |
| 5 | E |
| 6 | F |
| 8 | H |
-----------------------
I think (if I understood what you asked) you can try this : 我认为(如果我了解您的要求),您可以尝试以下操作:
SELECT norm.NormID, norm.NormName
FROM assignment
INNER JOIN norm ON assignment.NID = norm.NormID
LEFT JOIN wire ON assignment.LID = wire.WireID
WHERE assignment.LID= 109
AND wire.wireID IS NULL
ORDER BY norm.NormName;
Edit after your comments. 发表评论后进行编辑。 I think you could use:
我认为您可以使用:
SELECT A.NormID, A.NormName
FROM norm A
LEFT JOIN (SELECT NID FROM assignment WHERE LID = 109) B ON B.NID = A.NormID
WHERE B.NID IS NULL
ORDER BY A.NormName;
OR 要么
SELECT A.NormID, A.NormName
FROM norm A
WHERE NOT EXISTS (SELECT 1 FROM assignment WHERE LID = 109 AND ASSIGNMENT.NID = A.NormID)
ORDER BY A.NormName;
after you added a sample data the entries that are not setted with 109 wireid are these: 添加样本数据后,未使用109wireid设置的条目如下:
SELECT norm.NormID, norm.NormName
FROM assignment
inner JOIN norm
ON assignment.NID = norm.NormID
INNER JOIN wire
ON assignment.LID = wire.WireID
WHERE wire.WireID <> 109
ORDER BY norm.NormName;
Try this: 尝试这个:
select norm.NormID,norm.NormName from norm
Inner JOIN
assignment on assignment.NID = norm.NormID
where assignment.LID in(select wireID from Wire where WireID = 109)
Im not so sure coz i dont have your data 我不太确定,因为我没有您的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.