需要一个MYSQL查询来比较两个表并且只输出不匹配的结果

发布于 2024-08-29 07:50:46 字数 168 浏览 3 评论 0原文

我的数据库中有两个表,其中一个包含项目列表以及有关这些项目的其他信息。另一个表包含这些项目的照片列表。

items 表为每个项目提供了一个唯一的标识符,该标识符在 photos 表中用于标识哪个项目已被拍摄。

我需要输出未链接到第二个表中照片的项目列表。关于我如何做到这一点有什么想法吗?

I have two tables in my database, one contains a list of items with other information on these items. The other table is contains a list of photographs of these items.

The items table gives each item a unique identifier,which is used in the photographs table to identifier which item has been photographed.

I need to output a list of items that are not linked to a photograph in the second table. Any ideas on how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉薄对峙 2024-09-05 07:50:46
select i.*
from Items i
left outer join Photographs p on i.ID = p.ItemID
where p.ItemID is null
select i.*
from Items i
left outer join Photographs p on i.ID = p.ItemID
where p.ItemID is null
无风消散 2024-09-05 07:50:46
SELECT * FROM items WHERE id NOT IN (SELECT item_id FROM photos);

应该是你想要的

SELECT * FROM items WHERE id NOT IN (SELECT item_id FROM photos);

should be what you want

不喜欢何必死缠烂打 2024-09-05 07:50:46

如果一件物品有多张照片,请使用不同的。

SELECT * FROM items WHERE id NOT IN (SELECT distinct(item_id) FROM photos);

use distinct if one item has more than one photograph.

SELECT * FROM items WHERE id NOT IN (SELECT distinct(item_id) FROM photos);
你在我安 2024-09-05 07:50:46
SELECT id,name from tbl_item 
WHERE id NOT IN (SELECT distinct(tbl_item.id) FROM tbl_item INNER JOIN tbl_photo ON tbl_photo.pid=tbl_item.id)
SELECT id,name from tbl_item 
WHERE id NOT IN (SELECT distinct(tbl_item.id) FROM tbl_item INNER JOIN tbl_photo ON tbl_photo.pid=tbl_item.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文