MySQL簡單查詢語法

2021-08-27 21:07:15 字數 1686 閱讀 6181

1.專案中如何儲存日期時間資料

大體上有三種方式

(1)varchar儲存:不足:不便於比較大小

(2)date/time/datetime儲存:不足:不便於實現國際化

(3)bigint儲存:表示距離計算集元年的毫秒值,任何程式語言都可以把大數字轉換成為

日期時間

2.mysql中使用自增列

id int primary key auto_increment

自增列:只能用於整數列,且必須是主鍵列自增列無需手動賦值,會自動採用1/2/3....數列,在當前最大值基礎上+1

注意:sql標準中沒有此關鍵字,他是mysql所專有的!

mysql查詢

1.簡單查詢----只查詢特定的列

select 列名,列名 from 表名

2.簡單查詢----查詢所有的列

select * from emp;

3.簡單查詢----給列取別名

select 列名 as 別名,列名 as 別名 from 表名

select 列名 『別   名』,列名 '別  名』 from 表名

注意:給列名取別名用as關鍵字,且可以省略;別名中若有空格,

需要用''括起來

4.簡單查詢-----只顯示不同的記錄

select distinct 列名 from 表名;

5.簡單查詢-----在查詢執行計算

select 2/3;#0.667

select 列名*數字 from 表名

6.簡單查詢-----查詢結果集的排序

select * from 表明 order by 列名;#預設asc,ascendant 公升序

有小到大排序

select * from 表名 order by 列名 desc;#descendant降序,

兩次排序

select * from 表名 order by 列名 desc,列名 desc;

7.簡單查詢-----條件查詢

select * from 表名 where 列名=條件;

=等於 >大於 《小於 >=大於等於 <=小於等於 !=或<>不等於

is null 是空

is notnull 不是空

and 和

or 或

not 不是

between 在什麼之間

select * from 表名 where 列名 between 引數 and 引數;

in 在那些範圍內

select * from 表名 where 列名 in(引數,引數,引數);

not in 不再這個範圍內

select * from 表名 where 列名 not in(引數,引數,引數);

8.簡單查詢-----模糊條件查詢

select * from 表名 where 列名='%e%'#錯誤寫法

select * from 表名 where 列名 like '%e%'#正確寫法

sql萬用字元:下面兩個萬用字元必須與like組合應用

%  匹配任意多個任意字元

_  匹配乙個任意字元

9.簡單查詢------分頁查詢

分頁查詢:不同的資料庫實現分頁查詢語法各不相同,mysql中的分頁查詢是最簡單的

limit 分頁關鍵字

limit start,count;start開始,count個數

MySQL查詢語法

select distinct productname from custom where product price between 5 and 10 order by product price desc product name limit 3 offset 1 limit 限定從1行開始選出...

mysql推薦查詢 MySQL模糊查詢語法(推薦)

sql select from table name where field name like var result mysql query sql or die mysql提供標準的sql模式匹配,以及一種基於象unix實用程式如vi grep和sed的擴充套件正規表示式 模式匹配的格式。sql...

Mysql模糊查詢語法

一.mysql模糊查詢語法支援2種匹配格式 1.sql匹配模式 開發中應用最多的一種 2.正規表示式匹配模式 不推薦使用 1.sql匹配模式 開發中應用最多的一種 1 使用sql匹配模式,不能使用操作符 或 而是使用like或not like 2 使用sql匹配模式,mysql提供了2中萬用字元 表...