空值的查询

-- 查询姓名为null
select * from tb1 where 姓名 is null;
-- 查询姓名不为null
select * from tb1 where 姓名 is not null;

distict 去重

在 MySQL 中 distict 是对查询记录去重,不是对单个字段去重。

因此:

  • distict 要放在第一个字段的前面
  • distict 是对多个字段整体去重
  • 如果要对单个字段去重,需要结合group by
cust_name    |cust_country|cust_zip|
-------------+------------+--------+
Village Toys |USA         |44444   |
Kids Place   |USA         |43333   |
Fun4All      |USA         |42222   |
Fun4All      |USA         |88888   |
The Toy Store|USA         |54545   |

如上数据:

select  DISTINCT cust_name ,cust_country from Customers 
-- 结果
cust_name    |cust_country|
-------------+------------+
Village Toys |USA         |
Kids Place   |USA         |
Fun4All      |USA         |
The Toy Store|USA         |

select  DISTINCT cust_name ,cust_country,cust_zip from Customers 

-- 结果
cust_name    |cust_country|cust_zip|
-------------+------------+--------+
Village Toys |USA         |44444   |
Kids Place   |USA         |43333   |
Fun4All      |USA         |42222   |
Fun4All      |USA         |88888   |
The Toy Store|USA         |54545   |