0

質問があります

SELECT dmn_name AS domain, COUNT(*) AS newsletters, SUM(mwl_sended) AS emailsSent, SUM(IFNULL(nwl_ca, 0)/mwl_sended) * 1000 AS ecpm FROM domain
                JOIN newsletter USING(dmn_id)
                WHERE dmn_actif = 1
                AND nwl_sendedOn > DATE_SUB(NOW(), INTERVAL 1 MONTH)
                AND nwl_sendingEndedOn is NOT NULL
                GROUP BY dmn_name
                ORDER BY ecpm DESC
                LIMIT 5

ecpm が最も高い 5 つの結果が返されます。最高5つと最低5つを返すように変更するにはどうすればよいですか? クエリを 2 回実行することは避けたいと思います。

ありがとう

4

1 に答える 1

1

使用するUNION ALL

SELECT dmn_name AS domain, COUNT(*) AS newsletters, SUM(mwl_sended) AS emailsSent, SUM(IFNULL(nwl_ca, 0)/mwl_sended) * 1000 AS ecpm FROM domain
                JOIN newsletter USING(dmn_id)
                WHERE dmn_actif = 1
                AND nwl_sendedOn > DATE_SUB(NOW(), INTERVAL 1 MONTH)
                AND nwl_sendingEndedOn is NOT NULL
                GROUP BY dmn_name
                ORDER BY ecpm DESC
                LIMIT 5
UNION ALL
SELECT dmn_name AS domain, COUNT(*) AS newsletters, SUM(mwl_sended) AS emailsSent, SUM(IFNULL(nwl_ca, 0)/mwl_sended) * 1000 AS ecpm FROM domain
                JOIN newsletter USING(dmn_id)
                WHERE dmn_actif = 1
                AND nwl_sendedOn > DATE_SUB(NOW(), INTERVAL 1 MONTH)
                AND nwl_sendingEndedOn is NOT NULL
                GROUP BY dmn_name
                ORDER BY ecpm ASC
                LIMIT 5
ORDER BY ecpm DESC
于 2013-05-03T15:41:26.667 に答える