MySQL資料庫 : 查詢語句,串連查詢及外鍵約束

來源:互聯網
上載者:User

標籤:外鍵約束   enc   資料   TE   between   總數   desc   mys   必須   

查詢指定欄位        select 欄位1,欄位2 from 表名;

消除重複行(重複指的是結果集中的所有完全重複行)             select distinct 欄位1,欄位2... from 表名

比較子(< > != = )            select * from 表名 where id>4

邏輯運算子(and or not in)            select * from 表名 where id>4(條件1) and gender=1(條件2);

模糊查詢(like %任意多個字元 _任意一個字元)            select * from 表名 where name like ‘馬%‘;

範圍查詢(in between...and...)                   select * from 表名 where id in(1,2,3); 查詢id是1,2,3的行(非連續)

select * from 表名 where id between 1 and 5;(查詢id 1到5 的行,連續)

判空(is null) (非空 is not null)                 select * from 表名 where 條件 is null;

優先順序 (小括弧,not,比較子,邏輯運算子(and比or先運算))

彙總函式
(count:求總行數)             select count(*) from 表名 where...

(max,min:最大值最小值)              select max(id) from 表名 where...

(sum,avg:求和求平均值)              select sum(id) from 表名 where...

分組(group by)                  select gender count(*) from 表名 group by gender;(按性別分組並分別統計總數)

對分組結果進行篩選                (having) select gender count(*) from 表名 group by gender having gender=0;

where 是對 from 後的原始表進行資料篩選,having 是對group by 的結果進行篩選.

排序(order by預設升序asc降序desc)                   select * from 表名 where 條件 order by id desc;

分頁(limit)select * from 表名 where 條件 limit 從第幾條資料開始,擷取多少條資料

完整select語句關鍵字順序:

select distinct *
from 表名 inner(left,right) join 表名 on 關係
where ...
group by ...
order by ...
limit start,count

外鍵約束 建立表的時候 最後添加              foreign key(限制欄位) references 表名(欄位名)

建立外鍵                    alter table 表名 add constraint 外鍵名 foreign key(欄位) references 外鍵表名(欄位)

內串連查詢(inner join 串連表名 on 表名.欄位=串連表名.欄位) ps:兩張表必須要有一種關係

左串連(left join 以左表為準,左表資料全部顯示) 右串連同理(right join)

MySQL資料庫 : 查詢語句,串連查詢及外鍵約束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.