【问题标题】:mysql query to join 3 tables having no association in one of the tablesmysql查询以连接其中一个表中没有关联的3个表
【发布时间】:2016-07-14 04:25:54
【问题描述】:

SQLFIDDLE LINK

我有以下事件表。

活动

+---------+---------------+
| column  |     type      |
+---------+---------------+
| id      | int(11)       |
| title   | varchar(255)  |
| content | longtext      |
+---------+---------------+

event_attachment

+----------+---------------------------------+
|  column  |              type               |
+----------+---------------------------------+
| id       | int(11)                         |
| caption  | varchar(255)                    |
| type     | enum('AUDIO', 'VIDEO', 'IMAGE') |
| path     | varchar(255)                    |
| position | int(11)                         |
+----------+---------------------------------+

event_gallery

+---------------+---------+
|    column     |  type   |
+---------------+---------+
| event_id      | int(11) |
| attachment_id | int(11) |
+---------------+---------+

我可以从单独的查询中获取事件附件,例如 -

SELECT ea.id, ea.caption, ea.type, ea.path,  ea.position
FROM `event_attachment` ea
WHERE ea.id
    IN (SELECT eg.attachment_id FROM `event_gallery` eg WHERE eg.event_id = {$event_id})
ORDER BY ea.position ASC;

但我不能直接对event_attachmentevents 执行JOIN

我也可以得到event,它是单个附件 -

SELECT e.title , eg.attachment_id
FROM `events` e 
JOIN `event_gallery` eg
    ON e.id = eg.event_id
GROUP BY e.id 

我无法让上述两个查询一起工作,并在单个查询中提供整个事件详细信息及其附件。

我想要的是在每个事件的第一个位置获取事件列表以及事件附件。现在,一个事件可能会或可能不会附加。

期望的输出 -

+----+-------------------+-------------------------------------------------------+------------------------------------------+----------+--+
| id |       title       |                        content                        |                   path                   | position |  |
+----+-------------------+-------------------------------------------------------+------------------------------------------+----------+--+
|  1 | Barclay Delacruz  | Soluta minim eiusmod laborum minima cumque fugiat     | e2d80cd9-7e4f-4a3d-92b5-d5c600367499.jpg |       1  |  |
|  2 | Lesley Strickland | Rem culpa dolor doloremque modi nisi esse exercita... | 2b15b00f-f094-469b-a56f-de7460326110.jpg |       1  |  |
+----+-------------------+-------------------------------------------------------+------------------------------------------+----------+--+

注意问题可能听起来重复,因为英语不是我的第一语言,而且很难将整个场景放在一个问题中。

【问题讨论】:

  • "我不能直接在 event_attachment 和 events 上执行 JOIN" ??为什么不呢?
  • @Strawberry 我正在创建 sqlfiddle。稍后将使用链接更新问题
  • @Strawberry 我添加了带有示例数据和查询的 sqlfiddle。请看一看。
  • @Strawberry 请查看更新。

标签: mysql sql database join database-design


【解决方案1】:

这是基本的东西!

SELECT e.id event_id
     , e.title
     , CASE WHEN LENGTH(e.content) > 10 THEN CONCAT(TRIM(SUBSTR(e.content,1,7)),'...') ELSE e.content END content
     , ea.id attachment_id
     , CASE WHEN LENGTH(ea.caption) > 10 THEN CONCAT(TRIM(SUBSTR(ea.caption,1,7)),'...') ELSE ea.caption END caption
     , ea.type
     , CASE WHEN LENGTH(ea.path) > 10 THEN CONCAT(TRIM(SUBSTR(ea.path,1,7)),'...') ELSE ea.path END path
  FROM events e
  JOIN event_gallery eg
    ON eg.event_id = e.id
  JOIN event_attachment ea
    ON ea.id = eg.attachment_id
 WHERE ea.position = 1;

+----------+------------------+------------+---------------+------------+-------+------------+
| event_id | title            | content    | attachment_id | caption    | type  | path       |
+----------+------------------+------------+---------------+------------+-------+------------+
|        1 | Barclay Delacruz | Soluta...  |             1 | c          | IMAGE | e2d80cd... |
|        2 | Jeremy Ballard   | Sapient... |             4 | Ipsum q... | IMAGE | 2b15b00... |
+----------+------------------+------------+---------------+------------+-------+------------+

【讨论】:

  • 是的。这是正确的。它还省略了我需要的标题和内容,但不是路径,因为我还需要显示图像。谢谢。
  • 我这样做只是为了方便展示。在实践中,我会在表示层处理这类问题(例如,如果合适的话,使用一点 PHP)
【解决方案2】:

您可以为树表执行JOIN。类似的东西:

SELECT e.title, ea.attachment
FROM `event_attachment` ea
JOIN `event__gallery` ag 
ON ea.id = eg.attachment_id JOIN `event` e
ON eg.event_id = e.id
ORDER BY ea.position ASC;

如果这不是你想要的对不起,但我的英语最差,我理解这个问题。

编辑

我已经在你的 SQLFIDDLE 中测试了我的查询,我认为这就是你想要的:

SELECT e.id, e.title, e.content, ea.path, ea.position
FROM `event_attachment` ea
JOIN `event_gallery` eg 
ON ea.id = eg.attachment_id JOIN `events` e
ON eg.event_id = e.id
GROUP BY e.id
ORDER BY ea.position ASC ;

【讨论】:

  • 我添加了带有示例数据和查询的 sqlfiddle。请看一看。
  • 是的。确实如此。谢谢:) 在一定长度的字符之后我还需要省略号,所以接受的答案就是这样做的。它基本相同,但涵盖了我的用例。再次感谢
  • 好的:) 非常感谢,很抱歉,但我不明白你想要省略号!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多