下面的查询按预期正确提取了数据,但是如果lnk_cat_isrc中有多个项与表:

SELECT
                isrc.ISRC,
                isrc.Track_Name,
                isrc.ArtistName,
                isrc.TitleVersion,
                isrc.Track_Time,
                `isrc_performer`.`PerformerName` ,
                `performer_category`.`PerformerCategory` ,
                `isrc_performer`.`PerformerRole` ,
                `isrc`.`isrc_ID`,
                `isrc_performer`.`Perf_ID`

        FROM `isrc`

        LEFT JOIN `isrc_performer` ON (isrc.isrc_ID = isrc_performer.isrc_ID)
        LEFT JOIN `performer_category` ON (performer_category.PerfCat_ID = isrc_performer.PerfCat_ID)
        LEFT JOIN `lnk_cat_isrc` ON (lnk_cat_isrc.isrc_ID = isrc.isrc_ID)
        LEFT JOIN `catalogue` ON (catalogue.ID = lnk_cat_isrc.cat_id)
        ORDER BY   isrc_ID     desc LIMIT 0 , 10
        ";


我不能在catalogue上使用catalogue,因为isrc表对于一个isrc可以有多个执行者。

所以关系是这样的:
group by表中的几个项目可以具有isrc表中的几个相同项目。反过来,每个isrc_performercatalogue表中可以有多个条目。

我想要的是相对于每个isrc显示isrc中的所有对应数据,而不是对isrc表中的每个项目重复显示。
我还想显示所有其余的“空” isrcs(在isrc_performer表中没有任何数据的那些)

你能给我任何想法吗?

附言尽管我没有从isrc_performer表本身提取任何数据,但是当用户定义catalogue变量的搜索条件时,我仍在使用它按目录号进行搜索,因此我需要将其保留在查询中。
isrc_performer

------ UPD:

尝试以图形方式表示这三个表关系中的可能变化:

cat1 - isrc1 - performer1
       isrc2 - performer1
             - performer2
             - performer3

cat2 - isrc2 - performer1
             - performer2
             - performer3
     - isrc3 - performer2
             - performer4

cat3 - isrc4
     - isrc1 - performer1


UPD(已添加图片)

这是丝网印刷。如您在图片1上看到的,共有9行具有相同的catalogue号,但是有3位重复表演的演员Jason,David和Paul。

mysql - MySQL-唯一行,仅对应于三个表之一-LMLPHP

这是因为3个不同的目录项具有与图2相同的3个不同表演者的确切isrc

mysql - MySQL-唯一行,仅对应于三个表之一-LMLPHP

= 1(isrc)* 3(目录)* 3(表演者)=输出9行

我只想让$where_condition网格为每个表演者只显示此$where_condition = "catalogue.Catalogue LIKE '%test%' OR ISRC LIKE '%test%' OR Track_Name LIKE '%test%' OR ArtistName LIKE '%test%' OR TitleVersion LIKE '%test%' OR PerformerName LIKE '%test%' OR PerformerCategory LIKE '%test%' OR PerformerRole LIKE '%test%'";的3行。

最佳答案

---重新排列答案以将“最佳”选项放在首位。..但是这一切都是徒劳的。.没有返回来自lnk_cat_isrc或目录的任何数据,为什么对目录进行过滤会有所不同?我们返回所有isrc而不考虑任何过滤,因为它是左连接...

因此,在给定样本数据的情况下,这会带来什么疑问?

可能更优雅...(但不确定是否会更快),从存在中移开,只在子查询中使用一个distant,因此目录查询始终为每个isrc返回1行;解决了保留左连接的1-M问题,从而使isrc记录不在目录限制内。
返回所有isrc信息(如果存在),执行者信息(如果存在),执行者类别信息(如果存在)和目录信息(如果)(仅当与目录过滤器匹配时)。

SELECT isrc.ISRC
    , isrc.Track_Name
    , isrc.ArtistName
    , isrc.TitleVersion
    , isrc.Track_Time
    ,`isrc_performer`.`PerformerName`
    ,`performer_category`.`PerformerCategory`
    ,`isrc_performer`.`PerformerRole`
    ,`isrc`.`isrc_ID`
    ,`isrc_performer`.`Perf_ID`
FROM `isrc`
LEFT JOIN `isrc_performer`
  ON isrc.isrc_ID = isrc_performer.isrc_ID
LEFT JOIN `performer_category`
  ON performer_category.PerfCat_ID = isrc_performer.PerfCat_ID
LEFT JOIN (SELECT distinct lnk_cat_isrc.isrc_ID
           FROM `lnk_cat_isrc`
           INNER JOIN `catalogue`
             ON catalogue.ID = lnk_cat_isrc.cat_id
           WHERE...) DCat
   ON Dcat.isrc_ID = isrc.isrc_ID
ORDER BY   isrc_ID     desc
LIMIT 0 , 10;


如您所指出的,联接导致了问题。因此,消除联接并使用存在符号。因为您没有从目录中选择任何值,所以也可以使用Distinct。虽然存在应该更快。

快速但不包括所有isrc记录...(不确定为什么存在或不存在应该将它们带回...)

SELECT isrc.ISRC
     , isrc.Track_Name
     ,isrc.ArtistName
     ,isrc.TitleVersion
     ,isrc.Track_Time
     ,`isrc_performer`.`PerformerName`
     ,`performer_category`.`PerformerCategory`
     ,`isrc_performer`.`PerformerRole`
     ,`isrc`.`isrc_ID`
     ,`isrc_performer`.`Perf_ID`
    FROM `isrc`
    LEFT JOIN `isrc_performer`
      ON (isrc.isrc_ID = isrc_performer.isrc_ID)
    LEFT JOIN `performer_category`
      ON (performer_category.PerfCat_ID = isrc_performer.PerfCat_ID)
    WHERE EXISTS (SELECT *
                  FROM  `lnk_cat_isrc`
                  INNER JOIN `catalogue`
                    ON catalogue.ID = lnk_cat_isrc.cat_id
                   --and your other criteria
                  WHERE (lnk_cat_isrc.isrc_ID = isrc.isrc_ID)
                  )
     OR NOT EXISTS (SELECT *
                    FROM `lnk_cat_isrc`
                    WHERE lnk_cat_isrc.isrc_ID = isrc.isrc_ID
    ORDER BY isrc_ID desc
    LIMIT 0 , 10


或使用选择不同的简单明了的方法;但慢

 SELECT isrc.ISRC
     , isrc.Track_Name
     ,isrc.ArtistName
     ,isrc.TitleVersion
     ,isrc.Track_Time
     ,`isrc_performer`.`PerformerName`
     ,`performer_category`.`PerformerCategory`
     ,`isrc_performer`.`PerformerRole`
     ,`isrc`.`isrc_ID`
     ,`isrc_performer`.`Perf_ID`
  FROM `isrc`
  LEFT JOIN `isrc_performer`
    ON (isrc.isrc_ID = isrc_performer.isrc_ID)
  LEFT JOIN `performer_category`
    ON (performer_category.PerfCat_ID = isrc_performer.PerfCat_ID)
  LEFT JOIN `lnk_cat_isrc`
    ON (lnk_cat_isrc.isrc_ID = isrc.isrc_ID)
  LEFT JOIN `catalogue`
    ON (catalogue.ID = lnk_cat_isrc.cat_id)
   --AND (other criteria on catalog here, cause in a where clause you left joins will behave like inner joins)
  ORDER BY isrc_ID desc
  LIMIT 0 , 10;

关于mysql - MySQL-唯一行,仅对应于三个表之一,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45741161/

10-12 02:54