选择
①sql语言对于空值的判断是IS NULL
或者IS NOT NULL
eg:
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Create table If Not Exists Customers (id int, name varchar(255)) Create table If Not Exists Orders (id int, customerId int) Truncate table Customers insert into Customers (id, name) values ('1', 'Joe') insert into Customers (id, name) values ('2', 'Henry') insert into Customers (id, name) values ('3', 'Sam') insert into Customers (id, name) values ('4', 'Max') Truncate table Orders insert into Orders (id, customerId) values ('1', '3') insert into Orders (id, customerId) values ('2', '1')
select Name as Customers from Customers where Id not in (select CustomerId from Orders);
排序
②字符串不以某个首字母(以“M”为例)开头,使用通配符:“M%” not like “M%”
eg:
写出一个SQL 查询语句,计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以’M’开头,那么他的奖金是他工资的100%,否则奖金为0。返回的结果集请按照employee_id排序。
select employee_id, case when (employee_id % 2) <> 0 and name not like "M%" then salary else 0 end as bonus from Employees order by employee_id;
修改
③update交换某一列的值,例如男女性别互换
eg:一个 SQL 查询来交换所有的 ‘f’ 和 ‘m’ (即,将所有 ‘f’ 变为 ‘m’ ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。
# Write your MySQL query statement below update Salary set sex = case sex when "m" then "f" else "m" end;
④删除某一列中的重复数据且保留的是id值小的数据
eg:删除重复的邮箱
DELETE p1 FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id
字符串处理
⑤
字符串的转大小写函数UPPER()/LOWER()
字符串的拼接函数/截取函数CONCAT()/SUBSTRING()
eg:修复表名字
select user_id, CONCAT(UPPER(substring(name,1,1)),LOWER(substring(name,2))) as name from Users order by user_id;