复杂左连接变量(MYSQLI)

时间:2015-10-02 00:08:37

标签: php sql mysqli left-join

在Mysqli语句中遇到LEFT JOIN的问题。我有两个表,一个有各种事件的单个条目。第二个表包含该事件的所有图像。

我想得到什么:我想获得最后8个事件的单一回复,其中包含该事件的图像计数以及为该事件上传的最后一张图片。

问题:一个简单的LEFT JOIN对此很有帮助,但这是一个障碍......有时这些事件有多天。因此将两个表连接在一起的一件事是FOLDER字段。但是,如果超过一天,也会有所不同。这使得事件与图像的匹配更加困难。

视觉示例:

表:事件

Title      Folder          Days    Added
-------------------------------------------
Event1     2015-EventA     1       20151001
Event2     2015-EventB     2       20150901
Event3     2015-EventC     3       20150801

表格:图片

File         Folder             Date
---------------------------------------
imgA1.jpg    2015-EventA        20150930
imgA2.jpg    2015-EventA        20151001
imgB1.jpg    2015-EventB-1      20150910
imgB2.jpg    2015-EventB-2      20150901
imgC1.jpg    2015-EventC-1      20150910
imgC2.jpg    2015-EventC-2      20151001
imgC3.jpg    2015-EventC-2      20150930
imgC4.jpg    2015-EventC-3      20150927

我正在寻找的结果(记住它正在为该事件选择上次上传的图片):

Title      Folder          Days     File         IMGCount     IMGFolder
------------------------------------------------------------------------
Event 1    2015-EventA     1        imgA2.jpg    2            2015-EventA
Event 2    2015-EventB     2        imgB1.jpg    2            2015-EventB-1 
Event 3    2015-EventC     3        imgC2.jpg    4            2015-EventC-2

我尝试了什么:

SELECT t1.*, t2.IMGCount, t2.File, T2.Folder AS IMGFolder
 FROM Events as t1
 LEFT JOIN (select count(File) AS IMGCount, Folder, File
         FROM Images GROUP BY Folder ORDER BY Date DESC LIMIT 8)
AS t2
on t1.Folder LIKE t2.Folder
OR t1.Folder LIKE t2.Folder || '-1'
OR t1.Folder LIKE t2.Folder || '-2'
OR t1.Folder LIKE t2.Folder || '-3'
OR t1.Folder LIKE t2.Folder || '-4'
   ORDER BY Added DESC LIMIT 8

这个ALMOST有效...但是Event表重复了所有匹配的第一个Event的信息...所以我得到了具有正确的Image表信息但是重复的事件:

Title      Folder          Days     File         IMGCount     IMGFolder
------------------------------------------------------------------------
Event 1    2015-EventA     1        imgA2.jpg    2            2015-EventA
Event 1    2015-EventA     1        imgB1.jpg    2            2015-EventB-1 
Event 1    2015-EventA     1        imgC2.jpg    4            2015-EventC-2

我做错了什么?

2 个答案:

答案 0 :(得分:1)

  1. 这并不容易。首先选择总计图像。
  2. 其次为您提供每个活动的最新日期图片。
  3. 最后给你的是那张图片。
  4. <强> Sql Fiddle Demo

    SELECT imgC.*, imgFile.`File`
    FROM 
        (
          SELECT E.`Folder`, E.`Title`, E.`Days`, COUNT(I.`File`) as IMGCount
          FROM Events E
          JOIN Images I
           ON E.`Folder` = SUBSTRING(I.`Folder`, 1,11)
          GROUP BY E.`Folder`, E.`Title`, E.`Days`
        ) as imgC 
    JOIN 
        (
          SELECT E.`Folder`,  MAX(`Date`) as `LastDate`
          FROM Events E
          JOIN Images I
            ON E.`Folder` = SUBSTRING(I.`Folder`, 1,11)
          GROUP BY E.`Folder`
        ) as imgDate
    ON imgC.`Folder` = imgDate.`Folder`
    JOIN (
            SELECT E.`Folder`,  I.`File`, I.`Date`
            FROM Events E
            JOIN Images I
              ON E.`Folder` = SUBSTRING(I.`Folder`, 1,11)
          ) as imgFile
    ON imgDate.`Folder` = imgFile.`Folder`
    and imgDate.`LastDate` = imgFile.`Date`
    

答案 1 :(得分:1)

真的,你应该修复你的数据结构。您需要连接表的正确密钥(例如事件ID)。

但是,您可以通过汇总images来获取计数和最长日期然后加入images来执行您想要的操作:

SELECT e.*, t2.IMGCount, t2.File, T2.Folder AS IMGFolder
FROM Events e JOIN
     (select count(File) AS IMGCount,
             substring_index(Folder, '-', 2) as efolder, MAX(date) as maxdate
      from Images
      GROUP BY eFolder
      ORDER BY maxdate DESC
      LIMIT 8
     ) i
     ON i.efolder = e.folder JOIN
     images ii
     ON substring_index(ii.Folder, '-', 2) = e.folder and
        ii.date = i.maxdate;