0
点赞
收藏
分享

微信扫一扫

牛客MySQL复习

IT程序员 2022-02-03 阅读 42

练习直通门

非技术快速入门

  1. 查询多列 select 字段名 from 表名
select device_id, gender, age, university from user_profile;
  1. 查询所有列
SELECT id, device_id, gender, age, university, province FROM user_profile;
  1. 查询结果去重 相关博客
    使用distinct关键字去重:在需要去重的字段前加distinct
select distinct university FROM user_profile;
  1. 查询结果限制返回行数
  • where用于添加限制条件
    SELECT device_id FROM user_profile WHERE id<=2; 	
    
  • limit 添加限制条件
    SELECT device_id FROM user_profile limit 0, 2;
    
  1. 将查询后的列重新命名

    SELECT device_id as user_infos_example from user_profile limit 0,2;
    

    as可以省略

    SELECT device_id  user_infos_example from user_profile limit 0,2;
    
  2. 查找学校是北大的学生信息

SELECT device_id, university from user_profile where university='北京大学';
  1. 查找年龄大于24岁的用户信息
SELECT device_id, gender, age, university FROM user_profile where age>24;
  1. 查找某个年龄段的用户信息
  • 利用and
select device_id, gender, age from user_profile WHERE age>=20 and age <= 23;
  • 利用between
select device_id, gender, age from user_profile where age BETWEEN 20 and 23;
  1. 查找除复旦大学的用户信息
  • where
SELECT device_id, gender, age, university FROM user_profile where university != '复旦大学';
  • not in
SELECT device_id,gender,age,university FROM user_profile WHERE university NOT IN ('复旦大学');
  1. 用where过滤空值练习
SELECT device_id, gender, age, university from user_profile 
WHERE age!='';
SELECT device_id, gender, age, university from user_profile 
WHERE age is not NULL;
举报

相关推荐

------------------牛客

牛客 骑士

牛客--手套

能否获胜【牛客】

仿牛客项目

成绩排序-牛客

0 条评论