sql示例

放肆的年华 提交于 2020-04-05 21:06:46

-- table_1中有id,age; table_2中有id,sex。想取出id,age,sex 三列信息,-- 将table_1,table_2 根据主键id连接起来
select a.id,a.age,b.sex from (select id,age from table_1) a join (select id, sex from table_2) b on a.id =b.id

 ================想知道有多少去重的用户数

select count(*) from(select distinct id from table_1) tb

================将数值型的变量转化为分类型的变量?—— case when 条件函数

-- 收入区间分组

select id,(case when CAST(salary as float)<50000 Then '0-5万'when CAST(salary as float)>=50000 and CAST(salary as float)<100000 then '5-10万'when CAST(salary as float) >=100000 and CAST(salary as float)<200000 then '10-20万'when CAST(salary as float)>200000 then '20万以上'else NULL end from table_1;

==============concat( A, B...)返回将A和B按顺序连接在一起的字符串

select concat('www','.iteblog','.com') fromiteblog;--得到 www.iteblog.com

==============substr(str,0,len) 截取字符串从0位开始的长度为len个字符。

select substr('abcde',3,2) fromiteblog;-- 得到cd

 ==============

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!