1

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

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

查询一:

 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

4 回答 4

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"
于 2013-05-08T12:15:08.977 回答
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"

这样做比进行联合的好处是,基于联合的方法可能需要服务器方面付出两倍的努力——这取决于引擎及其优化方法。

于 2013-05-08T12:15:12.347 回答
0

您可以将两个查询与UNIONor结合起来UNION 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"
于 2013-05-08T12:15:29.683 回答
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"
于 2013-05-08T12:15:34.163 回答