MySQL-一个查询中的两个记录集,顺序和限制不同
我不知道我想做的是否可能,但让我来揭露我的问题。 我有一个这样的问题:MySQL-一个查询中的两个记录集,顺序和限制不同,mysql,sql,limit,Mysql,Sql,Limit,我不知道我想做的是否可能,但让我来揭露我的问题。 我有一个这样的问题: SELECT SUM(report.impressions) AS impressions, SUM(report.revenue) AS revenue, report.country AS country FROM report_table report WHERE date >= '2014-01-01' AND date <= '2014-01-3
SELECT
SUM(report.impressions) AS impressions,
SUM(report.revenue) AS revenue,
report.country AS country
FROM
report_table report
WHERE
date >= '2014-01-01'
AND date <= '2014-01-31'
GROUP BY report.country
ORDER BY revenue DESC
LIMIT 0 , 5
这个查询给了我5条收入最高的记录。我还想拥有5张印象数最高的唱片。这是此查询的结果:
SELECT
SUM(report.impressions) AS impressions,
SUM(report.revenue) AS revenue,
report.country AS country
FROM
report_table report
WHERE
date >= '2014-01-01'
AND date <= '2014-01-31'
GROUP BY report.country
ORDER BY impressions DESC
LIMIT 0 , 5
我的问题是,我不想执行同一个查询两次,因为总和必须重新计算。
有没有一种方法可以在不必两次求和的情况下获得收入前五名和印象前五名?我不需要两次获得相同的记录。或者什么是获得最高性能的最佳方法?因为我可以将我的两个请求合并,但这似乎不是最优化的
答复:
以下是我的问题的有效解决方案:
SELECT impressions,
revenue,
country,
@rn := if (@n = n,@rn + 1,1) AS seqnum,
@n := n AS dn
FROM (
SELECT SUM(report.impressions) AS impressions,
SUM(report.revenue) AS revenue,
report.country AS country,
n
FROM country_report report
cross join (select 1 as n union all select 2) n
cross join (select @rn := 0, @n := 1) m
WHERE date >= "2014-01-01" AND date <= "2014-01-31"
GROUP BY n,report.country
ORDER BY n,(case when n = 1 then revenue else impressions end) DESC
) r
WHERE if (@n = n,@rn + 1,1) <=5
这里有一种方法,你可以得到完整的列表,先按收入排序,然后按印象排序
SELECT SUM(report.impressions) AS impressions, SUM(report.revenue) AS revenue,
report.country AS country
FROM report_table report cross join
(select 1 as n union all select 2) n
WHERE date >= "2014-01-01" AND date <= "2014-01-31"
GROUP BY n, report.country
ORDER BY (case when n = 1 then revenue else impressions end) DESC;
现在的问题是从每组中获取前五个值。可以使用子查询执行此操作:
select impressions, revenue, country
from (SELECT SUM(report.impressions) AS impressions, SUM(report.revenue) AS revenue,
report.country AS country,
@rn := if (@n = n then @rn + 1 else 1 end) as seqnum,
@n := n
FROM report_table report cross join
(select 1 as n union all select 2) n cross join
(select @rn := 0, @n := -1)
WHERE date >= "2014-01-01" AND date <= "2014-01-31"
GROUP BY n.n, report.country
ORDER BY n.n, (case when n.n = 1 then revenue else impressions end) DESC
) r
where seqnum <= 5;
如果你愿意,你也可以通过这种方式获得最大的五个印象和收入。这将精确地计算您查询的次数
(SELECT
SUM(report.impressions) AS impressions,
report.country AS country
FROM
report_table report
WHERE
date >= '2014-01-01'
AND date <= '2014-01-31'
GROUP BY report.country
ORDER BY impressions DESC
LIMIT 0 , 5)
Union
(SELECT
SUM(report.revenue),
report.country
FROM
report_table report
WHERE
date >= '2014-01-01'
AND date <= '2014-01-31'
GROUP BY report.country
ORDER BY revenue DESC
LIMIT 0 , 5)
你需要最大的五个印象和他们的收入吗?我需要最大的五个印象和最大的五个收入。我不需要为5个最大的印象获得收入。马库斯:是的,我考虑过临时桌子。与我在问题中添加的解决方案相比,您认为临时表提供了最佳性能吗?谢谢你,这很有效!为了让第二个请求生效,我不得不改变一些事情。我将更新我的第一篇文章以备将来参考:当我再次查看查询时,我意识到我在select语句中遗漏了@n的赋值。