合并两个选择语句以返回一个结果 [英] combining two select statements to return one result

查看:69
本文介绍了合并两个选择语句以返回一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并两个视图表中两个选择查询的结果,并根据这些表执行计算.也许有一种使用if ... else的简便方法来执行查询-是否有任何指针?

I need to combine the results for two select queries from two view tables, from which I am performing calculations. Perhaps there is an easier way to perform a query using if...else - any pointers?

基本上,我需要在sql查询1中将条件除以'ar.time_ratio',然后在查询2中忽略它.

Essentially I need to divide everything by 'ar.time_ratio' under the condition in sql query 1, and ignore that for query 2.

SELECT 
gs.traffic_date,
gs.domain_group,
gs.clicks/ar.time_ratio as 'Scaled_clicks',
gs.visitors/ar.time_ratio as 'scaled_visitors',
gs.revenue/ar.time_ratio as 'scaled_revenue',
(gs.revenue/gs.clicks)/ar.time_ratio as 'scaled_average_cpc',
(gs.clicks)/(gs.visitors)/ar.time_ratio as 'scaled_ctr',
gs.average_rpm/ar.time_ratio as 'scaled_rpm',
(((gs.revenue)/(gs.visitors))/ar.time_ratio)*1000 as "Ecpm"
FROM
group_stats gs, 
v_active_ratio ar

WHERE ar.group_id=gs.domain_group

SELECT 
gs.traffic_date,
gs.domain_group,
gs.clicks,
gs.visitors,
gs.revenue,
(gs.revenue/gs.clicks) as 'average_cpc',
(gs.clicks)/(gs.visitors) as 'average_ctr',
gs.average_rpm,
((gs.revenue)/(gs.visitors))*1000 as "Ecpm"
FROM
group_stats gs, 
v_active_ratio ar

where not ar.group_id=gs.domain_group

推荐答案

使用UNION运算符.如果对您很重要,您可以添加另一列以显示行来自哪个查询.

Use the UNION operator. You can add another column to show which query a row came from if that's important to you.

http://dev.mysql.com/doc/refman/5.1/en/union.html

这篇关于合并两个选择语句以返回一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆