编辑-解决方案:

SELECT a.content as title, c.content as title_text, b.content as content FROM content_big AS b LEFT JOIN content_small AS a on a.id = 7 LEFT JOIN content_small AS c on c.id = 8 WHERE b.id = 1


我试图做这样的选择:


从表content_big中获取列内容,其中ID = 1
从表content_small中获取列内容,其中ID = 7
从表content_small中获取列内容,其中ID = 8


是否可以使用1 select获得这些数据?
我正在尝试此查询的一些变体:

 SELECT content_small.content as title, content_small.content as title, content_big.content as content
 FROM content_big LEFT JOIN
      content_small
      on content_small.id = 7 LEFT JOIN
      content_small
      on content_small.id = 8
 WHERE content_small.content = 1


但是我收到这个错误


  不是唯一的表/别名:“ content_small”

最佳答案

别名表上的联接,如下所示:

SELECT cs1.content as title, cs1.content as title, content_big.content as content
 FROM content_big LEFT JOIN
      content_small cs1
      on content_small.id = 7 LEFT JOIN
      content_small cs2
      on content_small.id = 8
 WHERE cs2.content = 1


这样就不会出现命名冲突。

关于mysql - MYSQL选择左联接-如何使用2个不同的ID从同一个表中获得2行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46965869/

10-16 21:25