SQLselect
SQL是查询语言
select * from data.dataanalyst;
(查询data数据库里的dataanalyst表的所有列数据)
select city from data.dataanalyst;
(查询data数据库里的dataanalyst表的city列数据)
select positionID,city from data.dataanalyst;
(查询data数据库里的dataanalyst表的city列和positionID列的数据且positionID列在前)
select * from data.dataanalyst
limit 10;
(查询data数据库里的dataanalyst表的前10行数据,sql语句结尾一定要有分号)
select * from data.dataanalyst
order by companyId
(查询data数据库里的dataanalyst表数据并按照companyId升序排列(小在上)如果后面有desc就是降序排列,注意中文会被归到一起,但不是根据拼音首字母排序的。)
select * from data.dataanalyst
where companyId = 4184
(查询data数据库里的dataanalyst表数据并展示companyId是4184的数据,注意如果要筛选等于汉字或者其他字符串,就需要加双引号了。)
select * from data.dataanalyst
where companyId < 4184
(查询data数据库里的dataanalyst表数据并展示companyId小于4184的数据)
select * from data.dataanalyst
where companyId between 100 and 1500
(查询data数据库里的dataanalyst表数据并展示companyId小于1500大于100的数据)
select * from data.dataanalyst
where companyId in (1331,1337)
(查询data数据库里的dataanalyst表数据并展示companyId是1331和1337的数据,汉字或者其他字符串要加引号)
select * from data.dataanalyst
where companyId not in (1331,1337)
(查询data数据库里的dataanalyst表数据并展示companyId不是1331和1337的数据)
select * from data.dataanalyst
where city = "上海"
and education = "本科"
(查询data数据库里的dataanalyst表数据并展示city是上海,education是本科的数据)
select * from data.dataanalyst
where city = "上海"
or city = "北京"
(查询data数据库里的dataanalyst表数据并展示city是上海或者是北京的数据)
and和or的高级用法
select * from data.dataanalyst
where city = "上海"
and (education = "本科"
or workyear = "1-3年");
(查询data数据库里的dataanalyst表数据并展示city是上海的数据并从中筛出education是本科或者是workyear是1-3年的数据)
select * from data.dataanalyst
where
(city = "上海"
and education = "本科")
or
(education = "硕士"
and city = "北京");
(查询data数据库里的dataanalyst表数据并展示city是上海education是本科的数据以及展示city是北京education是硕士的数据)
select * from data.dataanalyst
where
secondtyle like "%开发%"
(查询data数据库里的dataanalyst表数据并展示secondtyle里包含开发的数据)
select * from data.dataanalyst
where
secondtyle like "后端%"
(查询data数据库里的dataanalyst表数据并展示secondtyle里开头是后端的数据,结尾就是%在前即可)
sql group by
select city,count(positionId) from data.dataanalyst
group by city
(统计不同city的positionId数量,并展示city和总数的量)
select city,count(positionId),count(1),count(*) from data.dataanalyst
group by city
(统计不同city的positionId数量,注意count(positionId),count(1),count(*)三者是等价的,后两个是根据默认项计算,不过有些细微的差异就是包不包括空值)
select city,count(positionId),count(distinct companyId) from data.dataanalyst
group by city
(查找并统计city中的positionId的数量,并统计去重后的companyId数量)
select city,education,count(1) from data.dataanalyst
group by city,education
(查找并统计city和education一样的情况下的数量)
group by 高级用法
select city,count(positionId) from data.dataanalyst
group by city
haying count(positionId) >= 100
(haying等价于where专门用于统计后的筛选,查找并统计各个city里的positionId的数量,并展示职位数为100以上的数据)
select city,conut(positionId) from data.dataanalyst
where industryfield like "%电子商务%"
group by city
haying count(positionId) > 50
(先筛选所有industryfield中含有电子商务的,并查看和统计所有city中positionId数量大于50个的)
select city,conut(positionId) from data.dataanalyst
group by city
haying count(if(industryfield like "%电子商务%",1,null)) > 50
(第三句,是指当匹配到了电子商务,就记1,如果没有匹配,就是null,这个写法的数字和上面的是不一样的,因为没有筛选,最后conut出来的依然是全部的数量)
select
city,
count(positionId),
count(if(industryfield like %电子商务%,industryfield,null)),
count(if(industryfield like %电子商务%,industryfield,null)) / count(positionId)
from data.dataanalyst
group by city
(查询并统计同一city的positionId数量,统计industryfield中有电子商务的数量,统计industryfield中有电子商务的数量除以positionId的数量)
select
city,
count(positionId),
count(if(industryfield like %电子商务%,industryfield,null)),
count(if(industryfield like %电子商务%,industryfield,null)) / count(positionId)
from data.dataanalyst
group by city
having count(if(industryfield like %电子商务%,industryfield,null)) >= 10
order by count(if(industryfield like %电子商务%,industryfield,null))
(查询并统计同一city的positionId数量,统计industryfield中有电子商务的数量,统计industryfield中有电子商务的数量除以positionId的数量,并且只显示统计industryfield中有电子商务的数量大于10的数据,并且升序排列)
select
city,
count(positionId),
count(if(industryfield like %电子商务%,industryfield,null)) as emarket,
count(if(industryfield like %电子商务%,industryfield,null)) / count(positionId)
from data.dataanalyst
group by city
having emarket >= 10
order by emarket
(查询并统计同一city的positionId数量,统计industryfield中有电子商务的数量,统计industryfield中有电子商务的数量除以positionId的数量,并且只显示统计industryfield中有电子商务的数量大于10的数据,并且升序排列(注意这里用了as进行了简化,并且在select里无法替换))
SQL函数
select left(salary,1),salary from data.dataanalyst
(查询并展示,salary的第一个左边字符和salary)
select left(salary,1),locate("k",salary,3),salary from data.dataanalyst
(查询并展示,salary的第一个左边字符,salary列中从第3个位置开始包括第三个位置找k出现的位置,salary列的值)
select left(salary,locate("k",salary)-1),salary from data.dataanalyst
(查询并展示,salary的第一个k出现的位置之前的值,和salary的值)
select
left(salary,locate("k",salary)-1),
left(right(salary,length(salary)-locate("-",salary)),length(salary)-locate("-",salary)-1),
length(salary),
salary
from data.dataanalyst
(查询并展示,salary从左开始,第一个出现k的位置的前一位之前的值,salary从右开始-后面的值,并以此为基准,从左开始去除最后一位,并展示salary的长度,和salary的值)
select
left(salary,locate("k",salary)-1),
length(salary),
substr(salary,locate("-",salary)+1,length(salary)-locate("-",salary)-1),
salary
from data.dataanalyst
(substr(字符串,从哪里开始,截取长度)查询并展示,salary从左开始,第一个出现k的位置的前一位之前的值,salary值的长度,截取从-后一位开始截取总长度减去-位置的长度减一的长度(即从斜杠后算起一直到最后一位前面的长度))
sql子查询
查询结果可以作为新表,再次查询。
select * from
(select
left(salary,locate("k",salary)-1)) as bottom,
substr(salary,locate("-",salary)+1,length(salary)-locate("-",salary)-1) as top,
salary
from data.dataanalyst) as t
(最后的as t是给表起别名,没有别名就做不了查询。这里是把上面的那个表变成了一个新的表t,查询t的数据的。)
select (bottom + top)/2 from
(select
left(salary,locate("k",salary)-1)) as bottom,
substr(salary,locate("-",salary)+1,length(salary)-locate("-",salary)-1) as top,
salary
from data.dataanalyst) as t
(从这里可以看出,是可以直接调用子查询的别名的。查询出salary从左开始,第一个出现k的位置的前一位之前的值并将这列命名为bottom,查询出salary值的长度,截取从-后一位开始截取总长度减去-位置的长度减一的长度(即从斜杠后算起一直到最后一位前面的长度)并将这列命名为top,并将前面查询出的表作为子查询表,计算并展示出(bottom + top)/2 的结果)
select * from data.dataanalyst
where city in (
select city from data.dataanalyst
group by city having count(positionId) >= 100)
(先查询表中有哪些城市的positionId的数量大于等于100,再把总表中,city是这些城市的项目筛选出来。注意子查询select后面一定要有city,因为where xxx in筛选的它,xxx必须要和select xxx 对应)
子查询写的时候,建议先从最里面开始写,并且做好缩进,会比较省心。
sql join
(想知道唯一网的招聘职位有哪些,需要跨表分析,根据前面的知识点,可以使用子查询)
分享到: