当前位置: 移动技术网 > IT编程>数据库>Mysql > 基于sql语句的一些常用语法积累总结

基于sql语句的一些常用语法积累总结

2017年12月12日  | 移动技术网IT编程  | 我要评论

1.当某一字段的值希望通过其它字值显示出来时(记录转换),可通过下面的语句实现:
case type when '1' then '普通通道' when '2' then '高端通道' end as type
其中“type"是字段名,”1“,”2“是字段值

2.返回数据库中用户表的表单名:
select name from table where type = 'u' and status >=2

3.按拼音首字母排序:
select * from table order by 列名 collate chinese_prc_cs_as_ks_ws
首先,在这里的collate是一个子句,主要是定义排序规则,可应用于数据库定义或列定义;或应用于字符串表达式以应用排序规则投影。
语法是collate   collation_name。参数collate_name是应用于表达式、列定义或数据库定义的排序规则的名称。
•collation_name 可以只是指定的 windows_collation_name 或 sql_collation_name。
1.windows_collation_name 是 windows 排序规则的排序规则名称。参见 windows 排序规则名称。
2.sql_collation_name 是 sql 排序规则的排序规则名称。参见 sql 排序规则名称。
注:sql  server的排序规则平时使用不是很多,也许不少初学者还比较陌生,但有 一个错误大家应是经常碰到: sql server数据库,在跨库多表连接查询时,若两数据 库默认字符集不同,系统就会返回这样的错误:“无法解决 equal to 操作的排序规则冲突。”

4.按姓氏笔画排序:
select * from table order by customername collate chinese_prc_stroke_ci_as
注释如上。

5.指定值的范围查询:
1.stockname like ‘%[a-za-z]%'
2.stockname like '[^f-m]‘
其中
[]
指定值的范围
^ 排除指定范围

6.对查询结果随机排序:
select * from  table orders order by newid()

7.返回两个表中共有的所有记录:
select tablea.a tableb.b from tablea  inner join tableb as b ontablea.a= b.b
其中sql中as的用法这里就不做熬述。

8.等待时间再执行语句:

复制代码 代码如下:

waitfor delay '00:00:05‘
select * from studentinfowaitfor time '23:08:00

9.向一个表a中插入记录,并且插入的记录在a中不存在(通过一个字段来判断):

复制代码 代码如下:

insert into tablea (tracekey,mutesms,createtime,traceuser,tracetime,traceslot,traceduration)
select 'trace_timer',0,getdate(),mobileid,getdate(),'30','0' from tableb where corpid = 10001
and not exists (select traceuser from tablea ) and mobileid like '13' and len(mobileid) = 11

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网