[英]Mysql subquery fails, need help :)
I have a table PICTURES
: 我有一张桌子PICTURES
:
username varchar(50)
picture_id varchar(50)
datetime
...and I have a table FRIENDS
: ...我有一张桌子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
. 当您在网站上有朋友时,您的用户user_1
进入user_1
,而您的朋友用户user_2
进入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) 我想显示一个用户(user_1)的朋友的前5张照片
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! 正如您所看到的,子查询返回多行,所以...我需要您的帮助或建议来帮助我管理此解决方案!
Try using IN
instead of =
in WHERE p.username = (
. Since you're selecting up to 5 rows =
doesn't quite make sense. 尝试使用IN
代替WHERE p.username = (
。中的=
,因为您最多选择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;
I suggest you try a JOIN
instead: 我建议您改用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
This will give you the last 5 pictures from any of user_1
's friends 这将为您提供来自user_1
的任何朋友的最后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. 我假设您的意思是您想要每个朋友的最新5张图片,而不是所有朋友的图片中最新的5张图片。
This is one of the greatest-n-per-group
problems that appears so frequently on StackOverflow. 这是在StackOverflow上经常出现的greatest-n-per-group
问题之一。 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. 说明:对于属于我的一个朋友的每张照片p1
,计算属于同一朋友并且具有最近日期时间的照片。 The pictures that are in the most 5 recent must have fewer than 5 other pictures that are more recent. 这是在最5最近的图片必须少于5张是更近的 其他图片。
Try changing the WHERE p.username =(subquery)
to WHERE p.username in(subquery)
尝试将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; 在p的p用户名中选择p.picture_id,p.datetime, 在 p.username IN中选择图片(从f的f.user_2用户,在f.user_1 ='(ENTER USERNAME HERE)'中选择f.datetime DESC LIMIT 5) .datetime DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.