gpt4 book ai didi

mysql - 显示一个表中的条目列表,该表在多/链接表中没有条目

转载 作者:行者123 更新时间:2023-11-29 02:39:33 25 4
gpt4 key购买 nike

我正在开发一款音乐节应用。我有三个表,artistsvehiclesvehicleLink

艺术家是;

| id | name     |
|----|----------|
| 1 | Artist 1 |
| 2 | Artist 2 |
| 3 | Artist 3 |

车辆是;

| id | type | reg  |
|----|------|------|
| 1 | Car | REG1 |
| 2 | Van | REG2 |
| 3 | Car | REG3 |

vehicleLink 是;

| id | artist_id | vehicle_id |
|----|-----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |

我可以使用以下 MySQL 轻松显示所有艺术家车辆的列表;

SELECT v.id, v.type, v.reg, a.name AS artist_name
FROM vehicles v
JOIN vehicleLink l ON v.id = l.vehicle_id
JOIN artists a ON l.artist_id = a.id

这给;

| id | reg  | type | artist_name |
|----|------|------|-------------|
| 1 | REG1 | Car | Artist 1 |
| 2 | REG2 | Van | Artist 1 |
| 3 | REG3 | Car | Artist 2 |

我现在需要一个列表,其中 artist_id 还没有出现在 vehicleLink 中。例如打印尚未向我们提供车辆详细信息的人员列表。

我搜索了 StackOverflow 和 Google,但没有找到任何有用的东西,我只是直接在 phpMyAdmin 中尝试这个。例如添加下面的 WHERE NOT,返回 0 行;

where not exists (select 1 from artists where id = l.artist_id)

table 上艺术家有 id。表 vehicleLink 有 artist_id

我需要正确的查询来显示哪个 artists.id 没有出现在 vehicleLink 中,在这些示例中是艺术家 3

最佳答案

你应该尝试使用 not in the id with vehicles

select  a.name AS artist_name, a.id AS artist_id 
from artists a where a.id not IN (
SELECT a.id
FROM vehicles v
JOIN vehicleLink l ON v.id = l.vehicle_id
JOIN artists a ON l.artist_id = a.id

)

关于mysql - 显示一个表中的条目列表,该表在多/链接表中没有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com