1

因此,在我的查询中,我需要从不同位置转储结果。其中一些结果之前可能需要一个“<”或“>”符号。最终,此查询使用 INTO OUTFILE 导出,但该部分有效。发生的事情是,当我尝试将 gtlt 值的结果连接到参数的结果值时,总是会中断...我添加了 a、b、c 和 x,这样您就可以看到在哪里显示了什么...我很确定它必须与第二个 where 语句有关,但我无法弄清楚。

无论如何....这是我的代码的一个简化示例:

SELECT      
'VAR 1',
'VAR 2',
'VAR 3'
UNION   
(SELECT         
CONCAT(if(result.parameter = '10', result.gtlt,'a '),COALESCE(sum(if(result.parameter = '10', result.result, NULL)),'-')) as 'VAR 1',
CONCAT(if(result.parameter = '61', result.gtlt,'b '),COALESCE(sum(if(result.parameter = '61', result.result, NULL)),'-')) as 'VAR 2',
CONCAT(if(result.parameter = '78', result.gtlt,'c '),COALESCE(sum(if(result.parameter = '78', result.result, NULL)),'-')) as  'VAR 3'
FROM (  
    SELECT
        event.id,
        event.end_date,
        event_result.parameter,
        event_result.result,
        COALESCE(CAST(event_result.gtlt as CHAR), 'x') as gtlt,
        event.tag
    FROM event
    Inner Join event_result ON event_result.tag = event.tag
    Inner Join tblesdstations19 ON tblesdstations19.id = event.id
    WHERE (event.id =  '735' OR event.id =  '845' OR event.id =  '209') AND `event`.`end_date` >= '1998-01-13' AND `event`.`end_date` <= '2010-02-09') as result    
WHERE result.parameter = '10' OR result.parameter = '61' OR result.parameter = '78' group by result.id, result.end_date, result.tag
order by 
    result.id ASC, 
    result.end_date DESC )

这是返回的示例:

VAR 1   VAR 2   VAR 3
x17.8   b 9.49  c -
x25.8   b 9.21  c -
x17.5   b 29    c 0.36
x16.6   b 9.6   c 0.52
x26.2   b 7.4   c 0.49
x24.3   b 7.9   c 0.34
x27.5   b 9.9   c -
 a -    x7.9    c -
 a -    x8.2    c -
 a -    x5.7    c -
x16.8   b 2.16  c -
x22.2   b 1.84  c -
x26.2   b - c -
x22.7   b 0.67  c -
x18.5   b 19    c 0.15
 x14    b 1.3   c 0.91
x19.5   b 12    c 0.38
 a -    x1.2    c -
x23.5   b 0.74  c 0.92

如果我剥离我们的 VAR 1 和 2 选择以及它适用于 VAR 3 的位置......

SELECT 'VAR 3'
UNION   
(SELECT         
CONCAT(if(result.parameter = '78', result.gtlt,'c '),COALESCE(sum(if(result.parameter = '78', result.result, NULL)),'-')) as  'VAR 3'
FROM (  
    SELECT
        event.id,
        event.end_date,
        event_result.parameter,
        event_result.result,
        COALESCE(CAST(event_result.gtlt as CHAR), 'x') as gtlt,
        event.tag
    FROM event
    Inner Join event_result ON event_result.tag = event.tag
    Inner Join tblesdstations19 ON tblesdstations19.id = event.id
    WHERE (event.id =  '735' OR event.id =  '845' OR event.id =  '209') AND `event`.`end_date` >= '1998-01-13' AND `event`.`end_date` <= '2010-02-09') as result    
WHERE result.parameter = '78' group by result.id, result.end_date, result.tag
order by 
    result.id ASC, 
    result.end_date DESC )

给我...

VAR 3
x0.36
>0.52
>0.49
>0.34
x0.15
>0.91
x0.38
x0.92
>0.48

编辑:将参数添加到 GROUPED BY 列表会得到以下结果:

17.8     -   -
   -    9.49     -
25.8     -   -
   -    9.21     -
17.5     -   -
   -    29   -
   -     -  0.36
16.6     -   -
   -    9.6  -
   -     -  >0.52
26.2     -   -
etc...

它显示了“<”>“符号,但没有像我需要的那样给出行中的值。

4

1 回答 1

0

知道了!!!!我使用 GROUP_CONCAT 而不是 SUM 并将结果与​​子结果集中的“>”或“<”连接起来

COALESCE(GROUP_CONCAT(if(result.parameter = '10', result.result, NULL)),'-') as 'VAR 1',
...
CONCAT(COALESCE(result.gtlt,''),' ',result.result) as result,
于 2013-02-26T21:44:05.540 回答