mysql常用语句整理
来源:互联网 发布:迅捷网络登录 编辑:程序博客网 时间:2024/05/16 08:05
1.查看表中是否有字段值重复:
select * from user_num group by user HAVING count(user)>=2;
2.函数作为查询的条件
select * from 表名 HAVING SUM(字段名) = SUM(字段名);
或者
select * from 表名 group by 排序字段HAVING SUM(字段名) = SUM(字段名)
3.在数据库的使用中,有时候可以使用视图来简化sql语句
创建视图的语句如下:
create view view_name as select 字段名 from 表名 group by 排序字段 ,排序字段
4.通过生日计算年龄的sql:
- select
- id,
- DATE_FORMAT(birthday,"%Y-%m-%d") birthday,
- CURDATE() ,
- (year(now())-year(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') ) as age
- from
- t_user where id in (1,55000,55001);
4.通过身份证号码查询用户的地址和年龄
select case left(member_idcard,2)
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '未知'
end as 省份 ,
year(curdate())-if(length(member_idcard)=18,substring(member_idcard,7,4),if(length(member_idcard)=15,concat('19',substring(member_idcard,7,2)),null)) as 年龄,
case if(length(member_idcard)=18, cast(substring(member_idcard,17,1) as UNSIGNED)%2, if(length(member_idcard)=15,cast(substring(member_idcard,15,1) as UNSIGNED)%2,3))
when 1 then '男'
when 0 then '女'
else '未知'
end as 性别
FROM member
-------未完,待续!
----
- MySql常用语句整理
- MySQL常用语句【整理】
- mysql常用语句整理
- MySql常用语句整理
- 常用的MySQL语句整理
- MySql常用DML语句整理
- mysql 之 常用sql语句整理
- mysql-sql常用sql语句整理
- [SQL]常用的MySQL基本语句(整理中)
- MySQL的一些常用的SQL语句整理
- 资深数据库SQL工程师整理的常用MySQL语句
- 最常用MySQL数据库操作语法语句命令整理汇总
- 常用sql语句整理
- 常用SQL语句整理
- oracle常用语句整理
- 常用语句 整理
- SQL常用语句整理
- 常用SQL语句整理
- UILabel标签文字过长时的显示方式
- Codeforces Beta Round #61 (Div. 2) B. Petya and Countryside (DP)
- Dialy —— 2016.12
- XIB中设置UIView圆角
- Xcode常见编译错误以及解决方法汇总(持续更新)
- mysql常用语句整理
- abdroid fragment adapter 的详细介绍
- CodeForces 66A - Petya and Java(思维)
- 清除Xcode最近打开的工程
- jQuery源码阅读笔记——整体结构
- calephp 中redirect() 与flash() 的区别
- 二维数组
- android studio 中app崩溃后无法查看日志
- HttpClient 中文文档