SQL查詢操作

2021-06-17 15:14:54 字數 1739 閱讀 8908

1)select * from tablename

2)select (distinct,去除重複值;少用,影響效率) cou1,cou2 from tablename

3)select cou1 as 'cou1',cou2 (as,給查詢結果起別名;可以省略) 'cou2' from tablename

4)datediff:計算時間差值

5)字段連線:select cou1+cou2 as 'newcou' from tablename

('+'兩端的型別必須相同)

6)指標指向當前記錄;

7)=(相等), >, <, >=, <=, <>, !=, not, and, or, 

in, not in, between, not between, like, not like, is null, is not null

例子:判斷 name='張三'(字串用單引號)

8)where句逐條判斷

select * from studentinfo where credit < 4 

select * from studentinfo where stuname > '張三' (查詢名字在張三後面的)

9)select * from studentinfo where credit between 2 and 4

等價於:select * from studentinfo where credit >= 2 and credit <= 4

10)select * from studentinfo where credit in (2,4,5)

等價於:select * from studentinfo where credit=2 or credit=4 or credit=5

11)select * from studentinfo where *** is not null (查詢空值不能用'=')

12)select * from course order by coucredit desc (asc公升序,desc降序;預設為公升序)

13)select * from course order by coucredit desc, couno asc (當coucredit相同時,按couno排序;後面還可以再接)

14)select couno,couname,coucredit from course order by 2 (按couname排序)

15)select top 5 * from studentinfo (查詢前5條結果)

select top 50 percent couno,couname from studentinfo (查詢前50%的結果)

16)select * from course where coucredit>=4 order by coucredit (order by必須放在where子句後面,即先篩選再排序)

17)高階查詢:and, or, not, in, like與%,_,萬用字元

18)select * from studentinfo where stuname like '%三%' ('張%', '張_', '__', '[nr]%','[a-e]%', '[^bcd]%')

19)select * from studentinfo where stuname like '張%' or stuname like '李%'

等價於:select * from studentinfo where stuname like '[張李]%'

20)萬用字元%的轉義字元

ASP操作SQL模糊查詢

bool findtrue false sqlconnection con new sqlconnection server database tongxunlu integrated security true gridview1.visible false sqlcommand cmd new ...

SQL的查詢操作語句

近來在寫sql,發現能寫一些複雜的sql可以很大的提高工作的效率。今天趁著有時間整理一下,方便以後用到的時候查詢 1 對特定的條件加和操作 select sum 列名 from 表名 where 列名 條件 2 對加和的資料再次進行加和,並且使用資料的空表 select c1 c2 c3 from ...

SQL 操作列轉行查詢

題目 如下一張學生成績表 tb score t name t course t score 小明 語文 90 小明 數學 87 小明 英語 85 小紅 語文 92 小紅 數學 89 小紅 英語 95 要求,寫出合理的sql語句,得到下面的結果 姓名 語文 數學 英語 小明 90 87 85 小紅 9...