如何从连接结果中排除NULL行

时间:2014-10-26 17:33:24

标签: mysql

下面是我要从中生成输出表的表t1和t2。 我的斗争是不在输出中包含y1NULL的行。

+--------------+  +--------------+  
|      t1      |  |      t2      | 
+--------------+  +--------------+
| x0 | x1 | x2 |  |  x0  |   y1  |    
+--------------+  +--------------+
| a  | 10 | a  |  |  a   |  haha |
| b  | 11 |NULL|  |  b   |  NULL |
| c  | 12 | c  |  |  c   |  foo  |
| d  | 13 |NULL|  |  d   |  NULL |
+--------------+  + -----+-------+

+--------------+
|    output    |
+--------------+
|  x0  |  y1   |
+--------------+
|  a   |  haha |
|  c   |  foo  |
+--------------+

我试过了:

SELECT t1.x2, t2.y1
FROM t1
LEFT JOIN t2 ON t1.x0 = t2.x0

另外:在某些情况下,我的行不会说NULL,而只是空单元格。

2 个答案:

答案 0 :(得分:1)

你需要:

SELECT t1.x2, t2.y1
FROM t1
LEFT JOIN t2 ON t1.x0 = t2.x0
WHERE y1 IS NOT NULL AND y1 <> ''

答案 1 :(得分:0)

:此:

SELECT
t1.*,
t2.*
FROM t1, t2
WHERE
(t1.x2 IS NOT NULL AND t1.x2 <> '') AND (t2.y1 IS NOT NULL AND t2.y1 <> '')

会给你:

'1', 'a', '10', 'a', '1', 'a', 'haha'
'1', 'a', '10', 'a', '4', 'd', 'foo'