Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 13:24:20
Original
1014 people have browsed it

bitsCN.com

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

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!