MYSQL返回行

时间:2015-03-23 06:41:21

标签: mysql

我有一个带有mysql记录的表,如下所示,我想得到结果,如果在receivefmuser和topm中没有值那么将显示没有记录(表示0行),但是mysql不能用作mysql仍然算作1排?

select receivedfmuser, topm from tb_postatus where pono = 36 and receivedfmuser is not null and topm is not null


TABLE
|tid|pono|receivedfmuser|topm      |
|1  |36  |02/02/2015    |          |
|2  |27  |02/03/2015    |02/03/2015|

3 个答案:

答案 0 :(得分:1)

在上面的示例中,row1中topm中的值实际上是一个空字符串值,而不是NULL。这就是它满足not null条件并显示记录的原因。所以,使用以下

select receivedfmuser, topm from tb_postatus 
where pono = 36 and 
receivedfmuser != '' and 
topm != ''

这不显示具有NULL的行以及具有空字符串值的行。

答案 1 :(得分:0)

尝试以下查询

select receivedfmuser, topm 
    from tb_postatus 
    where pono = 36 
    and receivedfmuser is not null
    and receivedfmuser != ""
    and topm is not null
    and topm != ""

答案 2 :(得分:0)

如果在上面的示例中,您检查null的值是topm,则问题是您的查询,因为您的查询正在验证" receivedfmuser"因为不是空值。

select t.receivedfmuser, t.topm, t.pono
from test t
where `receivedfmuser` IS NOT NULL;

receivedfmuser,topm,pono

' 02/02 / 2015',NULL,' 36' ' 02/02 / 2015',' 02/03 / 2015',' 27'

然后将查询更改为" topm"

select t.receivedfmuser, t.topm, t.pono
from test t
where `topm` IS NOT NULL;

receivedfmuser,topm,pono

' 02/02 / 2015',' 02/03 / 2015',' 27'

结果显示1行,topm非空。

用你的例子:

select t.receivedfmuser, t.topm, t.pono
from test t
where `topm` IS NOT NULL and t.pono = 36;

receivedfmuser,topm,pono

不返回结果。

希望这有帮助。

是的,如果您检查的值为null实际上是一个空值,则查询将无效,因为mysql不会将null理解为空字符串。