1

我有一个表名作为 a_ad_display 我有下表

advId | Displaytime

我有一个带有以下列的 a_advertise 表

advertiseId| advertisename| mediaTypeId

我有一个带有以下列的 a_mediatype 表

mediaTypeId | medianame

1 | Image
2 | Animation
3 | video

a_display 表中有多个 advId 条目

我想用他们的 mediaType 获取广告显示的总和,比如 a_display 表中有多少视频、图像和动画类型的广告

我已经这样做了

SELECT 
  mdtype.medianame AS _type,
  SUM(mdtype.medianame = 'Image') AS _display 
FROM
  a_ad_display a 
  INNER JOIN a_advertise adv 
    ON adv.advertiseId = a.advId 
  INNER JOIN a_mediatype mdtype 
    ON mdtype.mediaId = adv.mediaTypeId 
GROUP BY medianame ;

但我得到了以下结果

在此处输入图像描述

但是如果只写 sum(distinct mdtype.medianame) 那么它会给我错误......那么我怎样才能在一个查询中获得图像视频和动画的所有值?

4

4 回答 4

3
SELECT
    m.medianame,
    COUNT(as.advId) as Total
FROM    a_mediatype as m
INNER JOIN a_advertise as a ON a.mediaTypeId = m.mediaTypeId
INNER JOIN  a_ad_display as ad ON ad.advId = a.advertiseId
GROUP BY m.mediaTypeId
于 2013-02-12T06:31:04.000 回答
1

试试这个...

SELECT mdtype.medianame as _type, SUM(mdtype.medianame) as _display
FROM a_ad_display a
INNER JOIN a_advertise adv ON ad.advertiseID=a.advId
INNER JOIN a_mediatype mdtype ON mdtype.mediaID=adv.mediaTypeId
GROUP BY _type
HAVING DISTINCT(_type)
于 2013-02-12T06:28:13.720 回答
1

你可以试试这个

SELECT m.mediaTypeId, 
       MIN(m.medianame) AS `type`, 
       COUNT(m.mediaTypeId) AS `count`
  FROM a_ad_display a LEFT JOIN 
       a_advertise d on d.advertiseId=a.advId LEFT JOIN 
       a_mediatype m on m.mediaId=d.mediaTypeId 
GROUP BY m.mediaTypeId
于 2013-02-12T06:31:30.257 回答
1

试试这个 :

SELECT
    m.medianame,
    COUNT(as.advId) as Total
FROM a_mediatype as m
INNER JOIN a_advertise as a 
ON a.mediaTypeId = m.mediaTypeId
INNER JOIN  a_ad_display as ad 
ON ad.advId = a.advertiseId
GROUP BY m.mediaTypeId
于 2013-02-12T06:45:43.860 回答