gpt4 book ai didi

mysql - 连接3个MySQL表以显示所有数据

转载 作者:行者123 更新时间:2023-11-29 13:40:41 26 4
gpt4 key购买 nike

我在 stackoverflow 上阅读了无数关于类似问题的帖子,但我正在努力解决我的问题并理解各种 MySQL JOIN 查询。我正处于学习 MySQL 的早期阶段,如果有任何帮助,我们将不胜感激。

我有 3 张 table :

  1. 拍卖
  2. 类别
  3. 很多

拍卖和类别仅分别具有唯一的主键和拍卖日期、类别标题列。

批处理显示批处理标题、批处理描述等...并使用各自的 ID 链接到拍卖和类别表。

我只是希望显示地段表,其中显示所有行,包括拍卖日期和类别标题。

我失败的尝试:

SELECT l.lotid, l.lotnumber, l.lottitle, l.lotdescription, c.categorytitle, a.auctiondate, l.estimatefrom, l.estimateto, l.photo, l.datecreated, l.lastmodified 
FROM lot l
JOIN category c ON l.categoryid = c.categoryid
JOIN auction a ON l.auctionid = a.auctionid
ORDER BY l.lotnumber;

这就像一个 WHERE 查询,我确信它应该这样做,省略 categorytitleauctiondate 不匹配的任何行

有简单的解决方法吗?

非常感谢

最佳答案

使用LEFT JOIN:

SELECT 
l.lotid,
l.lotnumber,
l.lottitle,
l.lotdescription,
IFNULL(c.categorytitle,'n/a'),
IFNULL(a.auctiondate,'n/a'),
l.estimatefrom,
l.estimateto,
l.photo,
l.datecreated,
l.lastmodified
FROM
lot l
LEFT JOIN category c
ON l.categoryid = c.categoryid
LEFT JOIN auction a
ON l.auctionid = a.auctionid
ORDER BY
l.lotnumber;

关于mysql - 连接3个MySQL表以显示所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128082/

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