Mysql 子查询失败,需要帮助:)
我有一个表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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在
WHERE p.username = (
) 中使用IN
而不是=
。由于您最多选择 5 行= 不太有意义。
Try using
IN
instead of=
inWHERE p.username = (
. Since you're selecting up to 5 rows=
doesn't quite make sense.我建议您尝试使用
JOIN
:这将为您提供
user_1
的任意好友的最后 5 张照片I suggest you try a
JOIN
instead:This will give you the last 5 pictures from any of
user_1
's friends我假设您的意思是您想要每个朋友的最新 5 张照片,而不是所有朋友照片中的最新 5 张照片。
这是 StackOverflow 上频繁出现的
每组最大
问题之一。通常问题是从每组中找到前一个一个,但是当您想要前 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: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.尝试将
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)
toWHERE 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;