合并2个SQL查询

时间:2013-05-08 12:11:56

标签: sql expressionengine

我有2个单独运行的sql查询,我想要做的是合并两个,这样我就可以在一个查询中提取两组数据。

查询正在使用ExpressionEngine查询模块,如下所示,我想在一个查询中提取total和total_2:

查询1:

 SELECT COUNT(exp_channel_data.entry_id) AS total
 FROM exp_channel_data 
 JOIN exp_channel_titles 
     ON exp_channel_titles.entry_id = exp_channel_data.entry_id
 WHERE field_id_207 != '' 
     AND status = 'open'
     AND exp_channel_data.channel_id = '18'
     AND author_id = "CURRENT_USER"

查询2:

 SELECT COUNT(exp_channel_data.entry_id) AS total_2
 FROM exp_channel_data
 JOIN exp_channel_titles
    ON exp_channel_titles.entry_id = exp_channel_data.entry_id 
  WHERE status = 'open' 
    AND exp_channel_data.channel_id = '18'
    AND author_id = "CURRENT_USER"

4 个答案:

答案 0 :(得分:0)

使用union

加入两个查询
SELECT COUNT(exp_channel_data.entry_id) AS total  FROM exp_channel_data JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id WHERE field_id_207 != '' AND status = 'open' AND exp_channel_data.channel_id = '18' AND author_id = "CURRENT_USER"

union

SELECT COUNT(exp_channel_data.entry_id) AS total_2 FROM exp_channel_data JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id AND status = 'open' AND exp_channel_data.channel_id = '18' AND author_id = "CURRENT_USER"

您可能需要添加另一个字段来区分您正在查看的查询

SELECT 'q1' source, COUNT(exp_channel_data.entry_id) AS total  FROM exp_channel_data JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id WHERE field_id_207 != '' AND status = 'open' AND exp_channel_data.channel_id = '18' AND author_id = "CURRENT_USER"

union

SELECT 'q2' source, COUNT(exp_channel_data.entry_id) AS total_2 FROM exp_channel_data JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id AND status = 'open' AND exp_channel_data.channel_id = '18' AND author_id = "CURRENT_USER"

答案 1 :(得分:0)

您可以像这样运行查询。鉴于您没有说过您正在使用哪种SQL方言,您的里程可能会有所不同。

SELECT SUM(CASE WHEN field_id_207 != '' THEN 1 ELSE 0 END) AS total, COUNT(exp_channel_data.entry_id) AS total_2
FROM exp_channel_data 
JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id 
WHERE status = 'open' 
AND exp_channel_data.channel_id = '18'" 
AND author_id = "CURRENT_USER"

执行此操作的好处是,基于联合的方法可能需要服务器的两倍努力 - 取决于引擎及其优化方法。

答案 2 :(得分:0)

您可以将两个查询与UNIONUNION ALL组合在一起,第一个删除重复项,而第二个则不会(因此第二个通常更快)。但通常更好(和更快)调整查询以同时选择两个结果。

在这种情况下:

SELECT
    COUNT(exp_channel_data.entry_id) AS count_total_entryid, 
    SUM(IF(field_id_207!='';1;0)) AS count_nonempty_entryid 
FROM exp_channel_data 
JOIN exp_channel_titles ON exp_channel_titles.entry_id = exp_channel_data.entry_id 
WHERE status = 'open'
    AND exp_channel_data.channel_id = '18' 
     AND author_id = "CURRENT_USER"

答案 3 :(得分:0)

SELECT 
    SUM(CASE WHEN field_id_207 != '' then 1 else 0 end) as total_1,
    COUNT(exp_channel_data.entry_id) AS total_2 
FROM exp_channel_data 
JOIN exp_channel_titles 
ON exp_channel_titles.entry_id = exp_channel_data.entry_id 
AND status = 'open' 
AND exp_channel_data.channel_id = '18' 
AND author_id = "CURRENT_USER"