我通过子查询从LEFT JOIN获得每行左侧的多个结果

时间:2017-03-27 14:58:17

标签: mysql sql join left-join

通过此查询,我应该从聊天表中获取我获得的每个协议的最新消息,以及包括商家名称等在内的所有信息。

我在子查询中使用GROUP BY解决了它,但这不是我想解决的问题,因为我不明白为什么它会起到正确的作用,为什么不这样做?它按照我在子查询中的意思命令它:

SELECT agreements.id, agreements.`date`, agreements.state, business.name, chat.message
FROM ((agreements JOIN
       business_admin
       ON agreements.business = business_admin.business AND business_admin.user = 1
      ) LEFT JOIN
     business
     ON business.id = agreements.business
    ) LEFT JOIN
    (SELECT agreements_chat.agreement, agreements_chat.message
     FROM agreements_chat
     WHERE origin = 0 
     ORDER BY agreements_chat.`date` DESC
    ) AS chat
    ON agreements.id = chat.agreement

非常感谢你的帮助,非常感谢你!

1 个答案:

答案 0 :(得分:1)

它不起作用,因为左连接中的子查询返回多行,因此您获得的行重复。

gsub(paste0("[\n\t\r]+?","delete",".*[\n\t\r]+"), "\n",c, ignore.case = TRUE, fixed = FALSE)
# [1] "beginning of string\tmiddle of string\t=\n\t\t-\tWhy does this disappear\t\nEnd of string"

请注意(根据@GordonLinoff评论),您的联接周围不需要使用括号。