SQL常用语句之查询

1.查询所有列

1select * from user_profile
2select 表列名 from 表名

select * from user_profile实际应用中一般写列名,很少用*号。select 表列名 from 表名

2.查询结果去重

两种思路

第一种:使用distinct(关键词distinct用于返回唯一不同的值)

1

select distinct university from user_profile;

第二种:使用group by按照university进行分组

1

select university from user_profile group by university;

3.查询结果限制返回行数

select device_id from user_profile order by id limit 2;

使用LIMIT限制结果集

LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。
LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。

检索记录行 6-10

1

SELECT FROM table LIMIT 5,5

检索前 5 个记录行

1

SELECT FROM table LIMIT 5

选取中间任意几行( LIMIT OFFSET)

#查询第n到m行

SELECT 数据列 FROM 数据表 LIMIT m-n offset n   

4.将查询后的列重新命名

select device_id as user_infors_example from user_profile limit 0,2;
这里主要是用到了 起别名关键字 as 以及组合限制查询 limit 索引,个数
其中as可以省略,索引为0可以省略
select device_id user_infors_example from user_profile limit 2;

 5.查找学校是北大的学生信息

两种方法:

1

2

select device_id,university from user_profile where university='北京大学'; //精准查询

select device_id,university from user_profile where university like '%北京%'; //模糊查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值