標籤:
關於mysql常用語句的整理,上一篇涉及到ddl、dml以及一些簡單的查詢語句。
1:mysql分頁查詢
select * from table_name limit 5,10; 從下標為5元素查詢,查詢10條記錄,注意:mysql下標從0開始
2:關聯查詢
select a.id,a.name,b.id,b.name from table_name a ,table_name b where a.id=b.id; 表a和表b以欄位id關聯查詢
3:比較下面語句
select * from table_name where column_name=column_value; 根據某個欄位的值查詢表資料
select * from table_name where column in ( column_value1,column_value2,.....); 當有多個值時,使用 in 來查詢資料
select * from table_name where column in (select column_value from table_name ); 進階子查詢,以查詢某張表的結果集作為條件查詢
4:建表要注意的問題
not null : 非空約束 某個欄位有此約束,那麼向表中插入值時,不可為空。
default ‘M‘ :預設值 建立某個表之後,預設值為‘M‘
primary key:設定主鍵約束,就是給某個欄位設定一個標記,auto_increment :自動遞增 步長為1
is null :判斷欄位為空白時,使用is 而不是=
5:模糊查詢 like _ %
select * from table_name where name like %user_;
%:代表任一字元 _ :代表一個字元
6: > < = != <> <= >= 邏輯判斷的符號,這些符號經常與 any all 混合使用,
7: exists 是否存在,判斷一個欄位在表中是否存在,例子如下
select * from emp e where empno>0 and exists(select ‘x‘ from dept d where e.deptno=d.deptno and d.loc=‘beijing‘);
mysql資料庫常用語句2