mysql資料庫常用語句2

來源:互聯網
上載者:User

標籤:

關於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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.