简体   繁体   English

MySQL从3个表进行多次联接

[英]MySQL Multiple Joins from 3 Tables

I need to select all records from assets_tbl (A). 我需要从asset_tbl(A)中选择所有记录。 Included with these records I need to have any lightboxes ( lightbox_name ) that are linked to the asset_id from assets_tbl where author = "scott@co.com" OR authorized_viewers includes "scott@co.com" 包括在这些记录中,我需要具有任何链接到asset_idassets_tbl灯箱( lightbox_name ),其中author = "scott@co.com"authorized_viewers包括"scott@co.com"

I think this is close to what I need but it returns multiple rows of the same record: 我认为这接近我的需要,但它返回同一记录的多行:

SELECT
  A.*,
  C.lightbox_name,
  C.author,
  C.authorized_viewers
FROM
  media_tbl A
  LEFT JOIN lightbox_assets_tbl B ON A.asset_id = B.asset_id
  LEFT JOIN lightboxes_tbl C 
    ON B.lightbox_id = C.id
    AND C.author = "scott@co.com"
  LEFT JOIN lightboxes_tbl D ON D.authorized_viewers LIKE "scott@co.com"
ORDER BY A.id DESC

Here are the tables: 表格如下:

lightboxes_tbl
+-----+----------------+---------------+---------------------+
|id   |lightbox_name   |author         |authoried_viewers    |
+-----+----------------+---------------+---------------------+
|100  | aircraft-types |scott@co.com   |jon@co.com,aj@co.com |
|101  | maintenance    |nicole@co.com  |jon@co.com           |
|102  | ramp           |nicole@co.com  |scott@co.com         |
+-----+----------------+---------------+---------------------+



lightbox_assets_tbl
+-----+-------------+-------------+---------------+----------+
|id   |lightbox_id  |asset_name   |asset_path     | asset_id |
+-----+-------------+-------------+---------------+----------+
|1    |100          |a321.jpg     |project1/imgs/ | 3700     |
|2    |100          |b757.jpg     |project1/imgs/ | 3444     |
|3    |101          |FlyBy.swf    |project4/imgs/ | 1444     |
|4    |102          |Door_757.swf |project5/imgs/ | 3701     |
+-----+-------------+-------------+---------------+----------+


assets_tbl
+-----+---------------------+-------------------------------------+
|asset_id   |asset_name           | asset_location                |
+-----------+---------------------+-------------------------------+
|3700       |a321.jpg             |Libraries\Library_Media\Images |
|200        |757_Taxi.swf         |Libraries\Library_Media\Images |
|3444       |b757.jpg             |Libraries\Library_Media\Images |
|1444       |FlyBy.swf            |Libraries\Library_Media\Images |
|3701       |Door_757.swf         |Libraries\Library_Media\Images |
+----------+---------------------+--------------------------------+

Here are the expected RESULTS of the query: 以下是查询的预期结果:

+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|asset_id   |asset_name           | asset_location                |lightbox_name     | author      | authorized_viewers     |
+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|3700       |a321.jpg             |Libraries\Library_Media\Images |aircraft-types    |scott@co.com |jon@co.com,aj@co.com    |
+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|200        |757_Taxi.swf         |Libraries\Library_Media\Images |NULL              |NULL         |NULL                    |
+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|3444       |b757.jpg             |Libraries\Library_Media\Images |aircraft-types    |scott@co.com |jon@co.com,aj@co.com    |
+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|1444       |FlyBy.swf            |Libraries\Library_Media\Images |NULL              |NULL         |NULL                    |
+-----------+---------------------+-------------------------------+------------------+-------------+------------------------+
|3701       |Door_757.swf         |Libraries\Library_Media\Images |ramp              |nicole@co.com|scott@co.com            |
+----------+---------------------+--------------------------------+------------------+-------------+------------------------+

SQL Fiddle SQL小提琴

Thanks! 谢谢!

I'm wondering why you need two joins to the lightboxes_tbl table. 我想知道为什么您需要两次连接lightboxes_tbl表。 It seems like the second reference to that table (alias D ) is unnecessary. 似乎第二次引用该表(别名D )是不必要的。 Seems like you could just use an OR . 似乎您可以只使用OR

As a demonstration, replicating the predicates in your query: 作为演示,复制查询中的谓词:

LEFT JOIN lightboxes_tbl C
       ON B.lightbox_id = C.id
      AND ( C.author = 'scott@co.com'
          OR C.authorized_viewers = 'scott@co.com'
          )

But given that authorized_user contains a comma separated list (ACCKKK!!!), I suspect you really want to look for an exact match to an item in the comma separated list. 但是,鉴于authorized_user包含逗号分隔的列表(ACCKKK !!!),我怀疑您真的想在逗号分隔的列表中查找与某项的完全匹配。 The LIKE comparison that currently have is equivalent to an equals comparison (to the entire contents of authorized_viewers column). 当前具有的LIKE比较等效于等于比较(与authorized_viewers列的全部内容相同)。 You could add '%' wildcard characters to search for the value as part of the string... 您可以添加'%'通配符来搜索值作为字符串的一部分...

But that approach is will also match strings containing eg ebscott@co.com , which may not be what you really want. 但是这种方法还将匹配包含ebscott@co.com字符串,这可能并不是您真正想要的。

You could use the FIND_IN_SET function to find an exact match within the comma separated list... 您可以使用FIND_IN_SET函数在逗号分隔的列表中找到完全匹配的内容...

LEFT JOIN lightboxes_tbl C
       ON B.lightbox_id = C.id
      AND ( C.author = 'scott@co.com'
          OR FIND_IN_SET('scott@co.com',C.authorized_viewers)
          )

Storing comma separated lists is a SQL anti-pattern. 存储逗号分隔的列表是一种SQL反模式。 I recommend Bill Karwin's book: SQL Anti-Patterns: Avoiding the Pitfalls of Database Programming 我推荐Bill Karwin的书: SQL反模式:避免数据库编程的陷阱

http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557 http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM