MYSQL选择左连接 - 如何使用2个差异来从同一个表中获取2行?

时间:2017-10-27 00:46:58

标签: mysql

编辑 - 解决方案:

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 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'

1 个答案:

答案 0 :(得分:0)

在联接上对表进行别名,如下所示:

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

然后你就不会有命名冲突。