Mysql结果总是在SELECT ... FROM(SELECT ...)中返回NULL

时间:2013-02-26 16:57:35

标签: mysql

因此,在我的查询中,我需要从不同位置转储结果。其中一些结果可能需要“<”或“>”在他们面前签字。最终,使用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...

显示“&lt;” “&gt;” 中sypbols但不提供我需要的行中的值。

1 个答案:

答案 0 :(得分:0)

GOT IT !!!!而不是SUM我使用GROUP_CONCAT并将结果与​​“&gt;”连接起来或“&lt;”在子结果集中

COALESCE(GROUP_CONCAT(if(result.parameter = '10', result.result, NULL)),'-') as 'VAR 1',
...
CONCAT(COALESCE(result.gtlt,''),' ',result.result) as result,