-1

我在mysql表中有下表

a_ad_display

╔═══════╦══════════╗
║ ADVID ║ ADTYPEID ║
╠═══════╬══════════╣
║     1 ║        1 ║
║     2 ║        1 ║
║     3 ║        4 ║
║     3 ║        3 ║
║     2 ║        2 ║
║     1 ║        4 ║
║     1 ║        3 ║
║     4 ║        1 ║
║     4 ║        1 ║
╚═══════╩══════════╝

a_mediatype

╔═════════╦═══════════╗
║ MEDIAID ║ MEDIANAME ║
╠═════════╬═══════════╣
║       1 ║ Animation ║
║       2 ║ video     ║
║       3 ║ Image     ║
╚═════════╩═══════════╝

a_ad_type

╔══════════╦════════╗
║ ADTYPEID ║ ADTYPE ║
╠══════════╬════════╣
║        1 ║ e Ad   ║
║        2 ║ b Ad   ║
║        3 ║ c Ad   ║
║        4 ║ d Ad   ║
╚══════════╩════════╝

a_advertise

╔═══════╦═════════╗
║ ADVID ║ MEDIAID ║
╠═══════╬═════════╣
║     1 ║       1 ║
║     2 ║       1 ║
║     3 ║       2 ║
║     4 ║       3 ║
╚═══════╩═════════╝

现在我要获取的内容如下...

adType  | Image | video  | Animation
eAd     |    0  |    2   |    2 ...
b Ad    |...
c Ad    |...
d Ad    |.....

并且对于我想要数据的所有广告类型都是明智的......任何人都可以指导我......

这就是我尝试过的...

SELECT Distinct
at.adtype,
COUNT(IF(am.medianame = 'Animation',1,0)) AS Animation,
COUNT(IF(am.medianame = 'Video',1,0)) AS Video,
COUNT(IF(am.medianame = 'Image',1,0)) AS Image
FROM a_ad_type as at
LEFT JOIN a_ad_display as ad ON at.adtypeId = ad.adtypeId
LEFT JOIN a_advertise as adv ON adv.advertiseId = ad.advId
LEFT JOIN a_mediatype as am ON am.mediaId = adv.mediaTypeId
GROUP BY at.adtype
4

3 回答 3

3
SELECT  a.adtype,
        SUM(CASE WHEN d.medianame = 'Image' THEN  1 ELSE 0 END) `Image`,
        SUM(CASE WHEN d.medianame = 'video' THEN  1 ELSE 0 END) `video`,
        SUM(CASE WHEN d.medianame = 'Animation' THEN  1 ELSE 0 END) `Animation`
FROM    a_ad_type a
        LEFT JOIN a_ad_display b
            ON a.adtypeId = b.adtypeId
        LEFT JOIN  a_advertise c
            ON b.advId = c.advId
        LEFT JOIN a_mediatype d
            ON c.mediaId = d.mediaId
GROUP   BY a.adtype

更新 1

如果您有多个值medianame并且您不希望它一一指定。您可以为此创建 *Dynamic SQL8,

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(CASE WHEN d.medianame = ''',
      medianame,
      ''' then 1 ELSE 0 end) AS `',medianame,'`'
    )
  ) INTO @sql
FROM a_mediatype;

SET @sql = CONCAT('SELECT   a.adtype, ', @sql, ' 
                  FROM  a_ad_type a
                          LEFT JOIN a_ad_display b
                              ON a.adtypeId = b.adtypeId
                          LEFT JOIN  a_advertise c
                              ON b.advId = c.advId
                          LEFT JOIN a_mediatype d
                              ON c.mediaId = d.mediaId
                  GROUP   BY a.adtype');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
于 2013-02-14T05:32:34.827 回答
2
 SELECT Distinct
at.adtype,

COUNT(CASE am.mediaId WHEN 1 THEN at.adtypeId END) As Animation,
COUNT(CASE am.mediaId WHEN 2 THEN at.adtypeId END) As Video,
COUNT(CASE am.mediaId WHEN 3 THEN at.adtypeId END) As Image
FROM a_ad_type as at
LEFT JOIN a_ad_display as ad ON at.adtypeId = ad.adtypeId
LEFT JOIN a_advertise as adv ON adv.advertiseId = ad.advId
LEFT JOIN a_mediatype as am ON am.mediaId = adv.mediaTypeId
GROUP BY at.adtype

这就是我解决问题的方法..如果有人发现它错了,请纠正我..谢谢

于 2013-02-14T05:33:18.520 回答
1

您需要使用常见的列连接您的表,例如

SELECT ad.adtype
      ,am.medianame
FROM   a_ad_type ad
       INNER JOIN
       a_ad_display add ON ad.adTypeId=add.adTypeId
       INNER JOIN
       a_advertise a ON add.advId=a.advId
       INNER JOIN
       a_mediatype am ON am.mediaId=a.mediaId

那么您需要旋转此查询的结果。透视是数据库特定的语法,我不熟悉如何在 mysql 中执行它(我总是需要在 SQL Server 中查找它!)但是如果你搜索它,我相信你会找到它。

于 2013-02-14T04:52:50.647 回答