我有两个表用户和相册。在 users 表中有user_id
主键。在其他表专辑中有多行,user_id
因为每次用户上传新专辑时,它都会user_id
作为外键上传。我只想选择一次user_id
与其他表(专辑)忽略其他结果集。
我怎样才能做到这一点?
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
我相信这会奏效,你没有给我很多信息。
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC
->ORDER BY
任何你想要的行。在这种情况下,选择最新的。