以前学的SQL语句都忘了,借力扣再熟悉下。
基本语句格式:
select
属性1,属性2,...,属性N
from
SQL表
where
条件语句1
and / or
条件语句2
...
;
一些小tricks:
属性重命名 - as
select name as customer from ...;
表重命名:
(select * from 表x)新名字
数字、字符串的比较:
>、<、=、!=、<> (i.e. 不包括)
NULL的处理 - is
属性x is/is not NULL
一些例题:
法1:
select
name, population, area
from
World
where
area >= 3000000
or population >= 25000000
;
法2:
select
name, population, area
from
World
where
area >= 3000000
union
select
name, population, area
where
population >= 25000000
;
select
product_id
from
Products
where
low_fats = "Y"
and recyclable = "Y"
;
法1:
select
name
from
customer
where
referee_id != 2
or referee_id is NULL
;
法2:
select
name
from
customer
where
referee_id <> 2
or referee_id is NULL
;
法1:
select
name as Customers
from
Customers
where
id not in (
select
customerId
from
Orders
) order_customer
;
法2:
select name as Customers
from
( select
Name, CustomerId
from
Customers
left join
Orders
on
Customers.id = Orders.id
) join_order
where
customerId is NULL
;