页次: 1
比较运算可以说很丰富的。返回的是 0, 或者 1或者 null
第一个: 等于 (=)
mysql> select 1=0 , 1=1, "2" =2, "3"="3" , "null" = null, null = null; +-----+-----+--------+---------+---------------+-------------+ | 1=0 | 1=1 | "2" =2 | "3"="3" | "null" = null | null = null | +-----+-----+--------+---------+---------------+-------------+ | 0 | 1 | 1 | 1 | NULL | NULL | +-----+-----+--------+---------+---------------+-------------+ 1 row in set (0.00 sec)
需要注意 "2" = 2 是真的, null = null 是假的。
第二个: <=> 比较, 值返回 0, 1 并且 null <=> null 返回1
mysql> select null <=> 0 , null <=> 1, null <=> null; +------------+------------+---------------+ | null <=> 0 | null <=> 1 | null <=> null | +------------+------------+---------------+ | 0 | 0 | 1 | +------------+------------+---------------+ 1 row in set (0.00 sec)
第三个: <>, != 不等于
mysql> select 1 != 1, null != null , 1 <> 1; +--------+--------------+--------+ | 1 != 1 | null != null | 1 <> 1 | +--------+--------------+--------+ | 0 | NULL | 0 | +--------+--------------+--------+ 1 row in set (0.00 sec)
第三个小于 : < , <=
mysql> select 1 < 0, 0 < 1; +-------+-------+ | 1 < 0 | 0 < 1 | +-------+-------+ | 0 | 1 | +-------+-------+ 1 row in set (0.00 sec)
第四个:>, >=
mysql> select 1 >= 0, 1 > 0; +--------+-------+ | 1 >= 0 | 1 > 0 | +--------+-------+ | 1 | 1 | +--------+-------+ 1 row in set (0.00 sec)
第五个 : 非空, is null, is not null
mysql> select 0 is null , 1 is null, 1 is not null, null is null, null is not null; +-----------+-----------+---------------+--------------+------------------+ | 0 is null | 1 is null | 1 is not null | null is null | null is not null | +-----------+-----------+---------------+--------------+------------------+ | 0 | 0 | 1 | 1 | 0 | +-----------+-----------+---------------+--------------+------------------+ 1 row in set (0.00 sec)
可以发现 只有空是空的
第六: 范围 between a and b
mysql> select 2 between 1 and 3; +-------------------+ | 2 between 1 and 3 | +-------------------+ | 1 | +-------------------+ 1 row in set (0.00 sec)
第六: 最小值 least (a, b, c ...)
mysql> select least (1, 2, 5, 0), least ('a', 2), least (0, '0'), least (0, null), least ('a', 'z'); +--------------------+----------------+----------------+-----------------+------------------+ | least (1, 2, 5, 0) | least ('a', 2) | least (0, '0') | least (0, null) | least ('a', 'z') | +--------------------+----------------+----------------+-----------------+------------------+ | 0 | 0 | 0 | NULL | a | +--------------------+----------------+----------------+-----------------+------------------+
最好只通类型的比较, 数 和字符串
第七: greatest 最大的
mysql> select greatest (1, 2, 5, 0), greatest ('a', 2), greatest (0, '0'), greatest (0, null), greatest ('a', 'z'); +-----------------------+-------------------+-------------------+--------------------+---------------------+ | greatest (1, 2, 5, 0) | greatest ('a', 2) | greatest (0, '0') | greatest (0, null) | greatest ('a', 'z') | +-----------------------+-------------------+-------------------+--------------------+---------------------+ | 5 | 2 | 0 | NULL | z | +-----------------------+-------------------+-------------------+--------------------+---------------------+ 1 row in set, 1 warning (0.00 sec)
第八: 范围比较 in, not in
mysql> select 2 in (1,5), 2 in (1,2,3), null in (2, null); +------------+--------------+-------------------+ | 2 in (1,5) | 2 in (1,2,3) | null in (2, null) | +------------+--------------+-------------------+ | 0 | 1 | NULL | +------------+--------------+-------------------+ 1 row in set (0.00 sec)
mysql> select null not in (1) , null not in (null); +-----------------+--------------------+ | null not in (1) | null not in (null) | +-----------------+--------------------+ | NULL | NULL | +-----------------+--------------------+ 1 row in set (0.00 sec)
ipbbs.net
离线
页次: 1