我在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