Mysql 子查询失败,需要帮助:)

发布于 2024-08-18 06:48:21 字数 688 浏览 7 评论 0原文

我有一个表PICTURES

username varchar(50) 
picture_id varchar(50) 
datetime 

...我有一个表FRIENDS

user_1 varchar(50) 
user_2 varchar(50)  
datetime 

当您在网站上有朋友时,您的用户名会出现在user_1中,您朋友的用户名位于 user_2 中。对于每个新朋友一个新行...

我想显示一个用户 (user_1) 的朋友的最后 5 张照片,

所以我尝试

SELECT p.picture_id, p.datetime
FROM pictures AS p
WHERE p.username = (
    SELECT f.user_2
    FROM friends AS f
    WHERE f.user_1 = '(ENTER USERNAME HERE)'
    ORDER BY f.datetime DESC
    LIMIT 5
)
ORDER BY p.datetime DESC;

并且如您所见,子查询返回不止一行所以...我需要您的帮助或建议来帮助我管理此解决方案!

I have a table PICTURES:

username varchar(50) 
picture_id varchar(50) 
datetime 

...and I have a table FRIENDS:

user_1 varchar(50) 
user_2 varchar(50)  
datetime 

When you have friends on the website your username goes in user_1, and your friend username's go in user_2. For each new friend a new row...

I want to show the 5 last pictures of the friends of one user (user_1)

so I try

SELECT p.picture_id, p.datetime
FROM pictures AS p
WHERE p.username = (
    SELECT f.user_2
    FROM friends AS f
    WHERE f.user_1 = '(ENTER USERNAME HERE)'
    ORDER BY f.datetime DESC
    LIMIT 5
)
ORDER BY p.datetime DESC;

And as you can see, the subquery return more than one row so... I need your help or suggestions to help me managing this solution!

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

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

发布评论

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

评论(4

最偏执的依靠 2024-08-25 06:48:21

尝试在 WHERE p.username = () 中使用 IN 而不是 =。由于您最多选择 5 行 = 不太有意义。

SELECT p.picture_id, p.datetime
FROM pictures AS p
WHERE p.username IN (
    SELECT f.user_2
    FROM friends AS f
    WHERE f.user_1 = '(ENTER USERNAME HERE)'
    ORDER BY f.datetime DESC
    LIMIT 5
)
ORDER BY p.datetime DESC;

Try using IN instead of = in WHERE p.username = (. Since you're selecting up to 5 rows = doesn't quite make sense.

SELECT p.picture_id, p.datetime
FROM pictures AS p
WHERE p.username IN (
    SELECT f.user_2
    FROM friends AS f
    WHERE f.user_1 = '(ENTER USERNAME HERE)'
    ORDER BY f.datetime DESC
    LIMIT 5
)
ORDER BY p.datetime DESC;
世界如花海般美丽 2024-08-25 06:48:21

我建议您尝试使用 JOIN

SELECT
    p.picture_id, p.datetime
FROM 
    friends AS f 
    INNER JOIN pictures AS p ON f.user_2 = p.username
WHERE
    f.user_1 = '(ENTER USERNAME HERE)'
ORDER BY
    p.datetime DESC
LIMIT 5

这将为您提供 user_1 的任意好友的最后 5 张照片

I suggest you try a JOIN instead:

SELECT
    p.picture_id, p.datetime
FROM 
    friends AS f 
    INNER JOIN pictures AS p ON f.user_2 = p.username
WHERE
    f.user_1 = '(ENTER USERNAME HERE)'
ORDER BY
    p.datetime DESC
LIMIT 5

This will give you the last 5 pictures from any of user_1's friends

霊感 2024-08-25 06:48:21

我假设您的意思是您想要每个朋友的最新 5 张照片,而不是所有朋友照片中的最新 5 张照片。

这是 StackOverflow 上频繁出现的每组最大问题之一。通常问题是从每组中找到前一个一个,但是当您想要前 5 个或其他数量时,我是这样解决的:

SELECT p1.*
FROM friends AS f
JOIN pictures AS p1 ON (f.user_2 = p1.username)
LEFT OUTER JOIN pictures AS p2 ON (p1.username = p2.username
    AND p1.datetime < p2.datetime)
WHERE f.user_1 = ?
GROUP BY p1.picture_id
HAVING COUNT(*) < 5;

说明:对于每张图片 p1属于我的一位朋友的照片,计算属于同一朋友且日期时间较近的照片。最近 5 张照片中最近的其他张照片必须少于 5 张。

I assume you mean you want the latest 5 pictures from each of the friends, not the latest 5 pictures among all the friends' pictures.

This is one of the greatest-n-per-group problems that appears so frequently on StackOverflow. Normally the problem is to find the top one from each group, but here's how I solve it when you want the top 5 or some other quantity:

SELECT p1.*
FROM friends AS f
JOIN pictures AS p1 ON (f.user_2 = p1.username)
LEFT OUTER JOIN pictures AS p2 ON (p1.username = p2.username
    AND p1.datetime < p2.datetime)
WHERE f.user_1 = ?
GROUP BY p1.picture_id
HAVING COUNT(*) < 5;

Explanation: for each picture p1 that belongs to one of my friends, count the pictures belonging to the same friend and with a more recent datetime. The pictures that are in the most 5 recent must have fewer than 5 other pictures that are more recent.

煮茶煮酒煮时光 2024-08-25 06:48:21

尝试将 WHERE p.username =(subquery) 更改为 WHERE p.username in(subquery)

SELECT p.picture_id, p.datetime FROM pictures AS p WHERE p.username IN (SELECT f.user_2 FROM Friends AS f WHERE f.user_1 = '(ENTER USERNAME HERE)' ORDER BY f.datetime DESC LIMIT 5) ORDER BY p.datetime DESC;

Try changing the WHERE p.username =(subquery) to WHERE p.username in(subquery)

SELECT p.picture_id, p.datetime FROM pictures AS p WHERE p.username IN (SELECT f.user_2 FROM friends AS f WHERE f.user_1 = '(ENTER USERNAME HERE)' ORDER BY f.datetime DESC LIMIT 5) ORDER BY p.datetime DESC;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文