douyingmou1389 2019-07-16 05:09
浏览 90
已采纳

无法在MySql中获得第三个表连接的不同和最大值

Schemas 
// First table
CREATE TABLE assignments (
    id int,
    uid int,
    comments varchar(255),
    assignmentdate date,
    status int
);

INSERT INTO assignments (id, uid, comments, assignmentdate, status) 
values (1, 6, 'a', '2019-07-15', 0), (2, 6, 'ab', '2019-07-15', 0),
(3, 6, 'abc', '2019-07-14', 0), (4, 6, 'abc', '2019-07-14', 1)
, (5, 7, 'xyz', '2019-07-14', 1), (6, 7, 'zyx', '2019-07-14', 1);

// Second table
CREATE TABLE users (
    id int,
    username varchar(255),
    status int
);

INSERT INTO users (id, username, status) 
values (6, 'user1', 0), (7, 'user2', 0),
(8, 'user3', 1);

// Third table
CREATE TABLE user_images (
    id int,
    uid int,
    imagename varchar(255),
    status int
);

INSERT INTO user_images (id, uid, imagename, status) 
values (1, 6, 'abc.jpeg', 0), (2, 6, 'def.jpeg', 0), (3, 8, 'ghi.png', 1);

what I'm looking for here is to get 1) distinct and latest row of table assignments which, 2) joins the table users and get a row and then joins, 3) distinct and latest row of table user_images.

So far i have gone through this answer

My trial query:

SELECT
   p.*,
   u.username,
   groupedpi.*
FROM
   assignments p
INNER JOIN(
   SELECT
       comments,
       MAX(id) AS latest
   FROM
       assignments
   WHERE
STATUS
   = 0
GROUP BY
   uid
) AS groupedp
ON
   groupedp.latest = p.id
LEFT JOIN users u ON
   p.uid = u.id AND u.status = 0
LEFT JOIN(
   SELECT
       uid,
       MAX(id) AS latesti,
       imagename
   FROM
       user_images us
   WHERE
STATUS = 0
GROUP BY
   uid
   order by id desc LIMIT 1
) AS groupedpi
ON
   groupedpi.uid = p.uid 

Output: enter image description here

The 3rd result I'm not getting, i.e I'm not getting the distinct and latest record of the third table while joining. Instead of abc.jpeg, I want to get def.jpeg.

  • 写回答

1条回答 默认 最新

  • dtbsezxw28056 2019-07-16 05:16
    关注

    MySQL is tripping you up here, because it automatically adds columns to GROUP BY if they aren't specified, so it's grouping the groupedpi subquery on imagename too - this will lead to duplicated rows. Remove the imagename column from the subquery (and the order by clause is irrelevant too) and have it just output the userid and the max image id

    If you want the image name, join the images table in again on images.id = groupedpi.latesti (In the main query not the subquery that is finding the latest image id)

    (Note that your screenshot says lastesti 2 but imagename abc- it's not the right pairing. ID 2 is def.jpg. When you want latest Id but also other data from the same row you can't do it in one hit unless you use an analytic (mysql8+) - you have to write a subquery that finds the max id and then join it back to the same table to get the rest of the data off that row)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊