常用sql語句

2021-07-24 08:42:58 字數 1741 閱讀 4301

w3c:

1. select語句

- select * from test.grade

- select * from test.grade order by score

- select * from test.grade order by score desc

- select * from test.grade order by score asc

- select * from test.grade limit 2

- select * from test.grade where course like 『語%』

- select * from test.grade where course like 『%文』

- select * from test.grade where score like 『%1%』

- select * from test.grade where score in(81, 100)

- select * from test.grade where score between 81 and 100

- select name as student, course as cour from test.grade where score in(81, 100)

-2. distinct語句

3. sql函式

- select count(*) as total from test.grade

- select count(name) as total from test.grade

- select count(distinct name) as total from test.grade

4. 不同的 sql join

- join(inner join): 如果表中有至少乙個匹配,則返回行

- left join(在某些資料庫中, left join 稱為 left outer join): 即使右表中沒有匹配,也從左表返回所有的行

- right join: 即使左表中沒有匹配,也從右表返回所有的行

- full join: 只要其中乙個表中存在匹配,就返回行

5. case條件選擇

- select *, case when score>=60 then 『pass』 else 『fail』 end as 『mark』 from temp

- select *, case when score >= 90 then 『優』 when score >= 70 then 『中』 else 『差』 end as 『level』 from test.grade

6. group by結合sql函式一起使用

- select sum(score) as total from test.grade (所有學生的所有成績相加)

- select name, sum(score) as total from test.grade group by name(按學生進行分組,不同學生的總成績)

7. 日期

- select now(), curdate(),curtime()

- select datediff(curdate(), date(『2016-10-31』)) (日期間差值)

sql常用sql語句

1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...

常用sql語句

t sql語句複製表的方法 我在sql server 2000中有現個資料庫datahr及demo,它們的結構是一樣,其它有乙個表名為 gbitem.現在我想將demo資料庫的表名 gbitem的全部內容複製到datahr資料庫的表名為 gbitem中。請問此t sql語句應該怎麼寫?謝謝高人指點!...

常用SQL語句

查詢 sp who 中的結果值。因為儲存過程不能查詢,先轉為臨時表再查詢。declare tb table spid varchar 100 ecid varchar 100 status varchar 100 loginame varchar 100 hostname varchar 100 b...