經常使用SQL語句(嵌套子查詢/隨機等等)具體整頓。本站提示廣大學習愛好者:(經常使用SQL語句(嵌套子查詢/隨機等等)具體整頓)文章只能為提供參考,不一定能成為您想要的結果。以下是經常使用SQL語句(嵌套子查詢/隨機等等)具體整頓正文
1.SQL 拔出語句獲得主動生成的遞增ID值
insert into Table1(Name,des,num) values ('ltp','thisisbest',10);
select @@identity as 'Id'
2.完成是1 或0 想顯示為男或女
select name,Sex=(case Sex when '1' then '男' when '0' then '女' end) from Tablename
3.嵌套子查詢
select a,b,c from Table1 where a IN (select a from Table2)
4.顯示文章、提交人和最初答復時光
select a.title,a.username,b.adddate from tablename a,(select max(adddate) adddate from tablename where tablename.title=a.title) b
5.隨機提取筆記錄的例子
SQL Server:Select Top 10 * From Tablename Order By NewID()
Access:Select Top 10 * From Tablename Order By Rnd(ID)
Rnd(ID) 個中的ID 是主動編號字段,可以應用其他任何數值來完成,好比用姓名字段UserName
Select Top 10 * From 表Order BY Rnd(Len(UserName))
MySql:Select * From 表Order By Rand() Limit 10
6.在統一表內找雷同屬性的記載
select UserID from Accounts_Users where UserName is not null group by UserID having count (*)>1
7.查詢種別一切的產物對應數據
SELECT CategoryName,ProductName FROM Categories LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID;
8.按規模查詢編號在2 到5 之間的用戶信息
select * from UserValue where UserID between 2 and 5;
9.日程支配提早5 分鐘提示
Select * from TabSchedule where datediff(minute,getdate(),開端時光)<5
10.得出某日期地點月份的最年夜天數
SELECT DAY(DATEADD(dd, -DAY('2008-02-13'),DATEADD(mm, 1, '2008-02-13'))) AS 'DayNumber'
11.按姓氏筆劃排序
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
12.通配符的一些用法
1.select * from tablename where column1 like '[A-M]%' 如許可以選擇出column 字段中首字母在A-M 之間的記載
2.select * from tablename where column1 like '[ABC]%' 如許可以選擇出column 字段中首字母是A 或許B 或許C 的記載
3.select * from tablename where column1 like '[A-CG]%' 如許可以選擇出column 字段中首字母在A-C 之間的或許是G 的記載
4.select * from tablename where column1 like '[^C]%' 如許可以選擇出column 字段中首字母不是C 的記載
13.復制表構造(只復制構造,源表名:a,目的表名:b)
select * into b from a where 1<>1 或 select top 0 * into [b] from [a]
14.復制表數據(復制數據,源表名:a,目的表名:b)
insert into b(Name,des,num) select Name,des,num from Table1;