MYSQL JOIN 同一张表

发布于 2024-11-27 01:30:29 字数 1141 浏览 1 评论 0原文

我目前有这样的查询设置:

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  INNER JOIN mps_contents AS image 
    ON topic.content_id = image.page_id 
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 
  AND image.display_order = '1'

这是因为我想将同一个表中的两行合并为一行。这是表格的简化设置

-----------------------------------------------------------
| page_id | content_id | title | location | display_order |
-----------------------------------------------------------
|    1    |     200    |  Foo  |   NULL   |     200       |
|    1    |     201    |  Baz  |   NULL   |     201       |
|   200   |     201    |  Bar  | jpg.jpg  |      1        |
-----------------------------------------------------------

And basically I want this result

---------------------------------
| content_id | title | location |
---------------------------------
|     200    |  Foo  | jpg.jpg  |
|     201    |  Baz  |   NULL   |
---------------------------------

基本上我想选择所有主题,然后还返回相应的图像(如果有)。我当前的查询仅返回具有关联图像的所有主题。我尝试了左外连接和右外连接,但似乎没有帮助。

I currently have this query set-up:

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  INNER JOIN mps_contents AS image 
    ON topic.content_id = image.page_id 
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 
  AND image.display_order = '1'

This is because I want to merge two rows from the same table in one row. This is a simplified setup of the table

-----------------------------------------------------------
| page_id | content_id | title | location | display_order |
-----------------------------------------------------------
|    1    |     200    |  Foo  |   NULL   |     200       |
|    1    |     201    |  Baz  |   NULL   |     201       |
|   200   |     201    |  Bar  | jpg.jpg  |      1        |
-----------------------------------------------------------

And basically I want this result

---------------------------------
| content_id | title | location |
---------------------------------
|     200    |  Foo  | jpg.jpg  |
|     201    |  Baz  |   NULL   |
---------------------------------

Basically I want to select all topics, then also return the corresponding image if any. My current query only returns all topics with associated images. I tried LEFT and RIGHT OUTER JOINS but it doesn't seem to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此岸叶落 2024-12-04 01:30:29

当您对 OUTER JOIN 进行过滤时,您必须在 ON 子句中或作为派生表进行过滤。当 image.display_order = '1' 位于 WHERE 中时,它将始终是 INNER JOIN

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  LEFT JOIN
  mps_contents AS image ON topic.content_id = image.page_id
             AND image.display_order = '1'  
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  LEFT JOIN
  (
   SELECT *
   FROM mps_contents
   WHERE display_order = '1'
  ) AS image ON topic.content_id = image.page_id
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 

When you filter on an OUTER JOIN, you have to filter in the ON clause or as a derived table. When image.display_order = '1' is in the WHERE, it will always be an INNER JOIN

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  LEFT JOIN
  mps_contents AS image ON topic.content_id = image.page_id
             AND image.display_order = '1'  
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 

or

SELECT 
  topic.content_id, 
  topic.title, 
  image.location 
FROM 
  mps_contents AS topic 
  LEFT JOIN
  (
   SELECT *
   FROM mps_contents
   WHERE display_order = '1'
  ) AS image ON topic.content_id = image.page_id
WHERE 
  topic.page_id = (SELECT page_id FROM mps_pages WHERE page_short_name = 'foo' ) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文