0

我有 3 张桌子:

用户

  • id_user
  • 姓名

用户图片

  • id_picture
  • id_user
  • 文件名
  • ord(图片顺序)

用户贴纸

  • id_sticker
  • id_user_from
  • id_user_to

这是我当前的 SQL 调用。

SELECT
    u.id_user,
    u.name,
    p.filename,
    s.id_sticker,
    FROM user AS u
    LEFT JOIN user_picture AS p ON p.id_user = u.id_user
    LEFT JOIN user_sticker AS s ON s.id_user_to = u.id_user
WHERE u.id_user = 1
ORDER BY (SELECT COUNT(*) FROM user_sticker AS s2 WHERE s2.id_user_to = u.id_user AND s2.id_sticker = s.id_sticker) 

我需要获取特定用户最常收到的贴纸以及用户的第一张可用图片(具有 ord = 1 的图片)。一个用户可以有很多贴纸(以及重复的贴纸)和很多图片。

4

1 回答 1

1

这是sql:

select
    u.id_user,
    u.name,
    (select p.filename from user_picture as p where p.id_user=u.id_user and ord=1) as userpicture,
    (select max(s.id_sticker) from user_sticker as s where s.id_user_to=u.id_user) as userstickerid
from user as u
where u.id_user = $$$$
于 2013-10-14T23:06:33.813 回答