gpt4 book ai didi

使用 2 个内连接时 MySQL 查询未返回预期行数

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

在查询时遇到一些问题,我希望你们都能帮助我看看我缺少什么。COLLECTIONS 中有三行专辑的值为 1。但无论出于何种原因,查询仅返回 1 行而不是 3 行。

有什么想法吗?

SELECT a.id assetID
, a.uri
, a.caption
, u.alias
, u.icon
, u.link
, u.id userID
FROM collections c
JOIN assets a
ON a.id = c.asset
JOIN users u
ON u.id = a.user
WHERE c.album = 1
ORDER
BY a.ts DESC

数据结构如下

收藏

album   asset
1 1
2 2
1 3
1 4
2 5

Assets

id  uri                         user    private new ts          type    caption
1 1-1391997727-15835000.jpg 1 public 0 1368847153 image this is a test file thingy
2 2-1391997760-42134600.jpg 1 public 0 1368847353 image more test files WOOHOO 0 0
3 3-1391997811-96846600.jpg 3 public 0 1368847553 image i took this picture wooo
4 3-1391997840-97233100.jpg 3 public 0 1368848232 image
5 3-1391997859-93753900.jpg 3 public 0 1368848781 image w00t

用户

id  alias       link    icon
1 dbunting NULL NULL

最佳答案

您在结果集中得到一行,因为表 Assets 中有一个与用户 ID 1 匹配的行。

这是一个关于连接如何工作的很好的链接:What is the difference between Left, Right, Outer and Inner Joins?

此查询:

SELECT 
c.album,
a.id assetID
, a.uri
, a.caption
, u.alias
, u.icon
, u.link
, u.id userID
FROM collections c
INNER JOIN assets a ON (a.id = c.asset AND c.album=1)
LEFT JOIN users u ON u.id = a.user

这将输出专辑 1 的 3 行

玩吧:http://www.sqlfiddle.com/#!2/00b0f/1

关于使用 2 个内连接时 MySQL 查询未返回预期行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21817788/

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