1. 程式人生 > >sql 語句(精品)

sql 語句(精品)

GROUP BY:

select avg(latency),projectName,data_trunc('hour'm\,_time_) as hour group by projectName,hour.

 

特殊條件:

1、空值判斷:is null

Select * from table_Name where comm is null;

2、between and(範圍之間的值)

Select * from table_Name where sal between 100 and 300;

3、In(類似C++中的列舉)

Select * from table_Name where sql in (100,200,300)

4\like

Like模糊查詢

Select * from table_Name sal like "%M";

%表示多個字值,_下劃線表示一個字元。

二、不帶比較運算子的where子句。

SELECT studentNO FROM student WHERE 0;

則回返回一個空集,因為每一行記錄WHERE都返回false。

SELECT studentNO FROM student WHERE 1;

返回student表所有行中studenNO列的值。因為每一行記錄WHERE 都返回true。

 

1)AND & OR

AND & OR運算子用於基於一個以上的條件

2)ORDER BY(預設是ASC)

ORDER BY 關鍵字用於對結果進行排序。

  1、用於對結果集按照一個列或多個列進行排序。

  SELECT * FROM Websites ORDER BY country,alexa;(按照country再按照alexa來排序)。

  2、預設按照升序對記錄進行排序,如果需要按照降序對記錄進行排序,那要使用DESC關鍵字。

ORDER BY 多列的時候,先按照第一個column name排序,再按照第二個column name排序。

order by A,B;//預設按升序排。

order by A desc,B;//這時候A降序,B升序排列。

order by A , desc B;//這時候A升序,B降序。

3)INSERT INTO 語句用於向表中插入新記錄。

有兩種編寫形式。

第一種形式無需指定要插入資料的列名,只需提供被插入的值即可。(沒有指定要插入的列名的形式需要列出插入行的每一行資料).

INSERT INTO TABLE VALUE(value1,value2,value3);

第二種形式需要指定列名以及被插入的值。

INSERT INTO TABLE (column1,column2,column3,...) VALUES (value1,value2,value3);

 

insert into select 和 select into from

insert into TABLE_A select  * from TABLE_B where neza='neza'      --插入一行,要求表TABLE_A 必須存在。

select * into scorebak from where neza = 'neza';          --也是插入一行,要求scorebak不存在。

 

4)UPDATE語句

 

UPDATE table_name SET column1 = value1,column2 = value2,... WHERE some_column = some_value;

執行沒有where子句的update要慎重。(在MySql中可以通過設定sql_safe_updates 這個自帶的引數來解決,當該引數開啟的情況下,你必須在update語句後攜帶條件where,否則就會報錯)。

 

 

 

&n