〇、建表数据
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32) NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');
一、基础查询
1、查询所有列
注意:不要用*,而是写全部列名
select
id, device_id,
gender, age,
university, province
from user_profile;
2、查询指定列
二、简单处理查询结果
3、查询结果去重
select
DISTINCT university
from user_profile;
注意:DISTINCT是去重关键字
4、查询结果限制返回行数
前n个:SQL的最后加LIMIT n
查询前2个用户的明细设备id
select
device_id
from user_profile
LIMIT 2;
5、将查询后的列重新命名
select
device_id as user_infos_example
from user_profile
LIMIT 2;
本文来自博客园,作者:哥们要飞,转载请注明原文链接:javascript:void(0)p/16156739.html