你可能不知道的SQL问题
1. 如下是sql语句和结果,
SELECT count(*) AS total FROM orders; +-------+ | total | +-------+ | 150 | +-------+ SELECT count(*) AS cust_123_total FROM orders WHERE customer_id = '001'; +----------------+ | cust_123_total | +----------------+ | 65 | +----------------+
请分析下面SQL的执行结果:SELECT count(*) AS cust_not_123_total FROM orders WHERE customer_id <> '001'
解析: 第一眼看上去觉得很简单吧, 这个结果就是85嘛, 但是实际上可能不是, 因为customer_id <> '001' 这个条件不包含
customer_id值为null的情况,也就是说这个结果是小于等于85.
2. 来看下一个,你可以直接分析出来吗?
sql> SELECT * FROM runners; +----+--------------+ | id | name | +----+--------------+ | 1 | John Doe | | 2 | Jane Doe | | 3 | Alice Jones | | 4 | Bobby Louis | | 5 | Lisa Romero | +----+--------------+ sql> SELECT * FROM races; +----+----------------+-----------+ | id | event | winner_id | +----+----------------+-----------+ | 1 | 100 meter dash | 2 | | 2 | 500 meter dash | 3 | | 3 | cross-country | 2 | | 4 | triathalon | NULL | +----+----------------+-----------+
下列语句的结果是什么?
SELECT * FROM runners WHERE id NOT IN (SELECT winner_id FROM races)
解析: