MySql學習,mysql學習筆記

來源:互聯網
上載者:User

MySql學習,mysql學習筆記
資料庫查詢設定兩張資料庫表

  • 第一個表格user包含:
user_id username age sex
1 Alps1992 22 man
  • 第二個表格toy包含
user_id toyname
1 OnePiece
普通查詢:查詢關鍵字:

AS, SUM, DESC, GROUP BY, ORDER BY, AVG, MIN, MAX, COUNT, LIMIT;

關鍵字查詢例子

select * from user as u order by age limit 5;
// as 用來做別名, order by 用來排序(從小到大,從大到小是DESC), limit用來取結果的條數。

select sum(age) from user group by sex;
// sum分別求出user中男性年齡的和,還有女性的年齡和,group by分組。

avg是求平均, min是求最小值, max是最大值,和sum用法一樣

select count(user_id) from user;
//返回user_id個數,也就是表中人數。

//查詢所有人的資訊,並按照年齡排序:
select * from user order by age;

//查詢名叫chen的資訊:
select * from user where username='chen';

//查詢年齡為18的人的姓名,並讓id從大到小排序
select username from user where age=18 order by user_id DESC;

子查詢:關鍵字:

IN, NOT IN, EXISTS, NOT EXISTS

//查詢所有擁有toyname為pig的人的姓名:
select username from user where user_id in (select user_id from toy where toyname='onepiece');

聯結查詢:首先交叉串連:cross join

select u.username, t.toyname from user as u cross join toy as t;
//交叉串連就是把所有第一個表和第二個表的值一一對應
//例如:假如第一個表格有5個值,第二個表有4個值,這個結果就應該有20條記錄。

內聯結:inner join相等串連:(equal join)

select u.username, t.toyname from user as u inner join toy as t on(也可以用where) u.user_id = t.user_id;
//這個的結果就是各自擁有的玩具了。

不等串連:(non-equal join)

select u.username, t.toyname from user as u inner join toy as t on(也可以用where) u.user_id <> t.user_id order by u.username;
//這個的結果是各自沒有的玩具

自然串連:(natural join)

這個是有在串連的兩個表格中的列名稱相等的時候才能用。

select u.username, t.toyname from user as u natural join toy as t; //和上面的相等串連的結果是一樣的。
外串連:outer join

左外聯結、右外聯結比較相似:left outer join ; right outer join;

select u.usrename, t.toyname from user u inner join toy t on (這裡不能用where) u.user_id = t.user_id;//這個是查詢使用者姓名和他所擁有的玩具名字

select u.username, t.toyname from user u left outer join toy t on u.user_id = t.user_id;
//這個是以左表(user)為基準,來查詢所有使用者姓名和他所擁有的玩具名字。

select u.username, t.toyname from user u right outer join toy t on u.user_id = t.user_id;
//這個是以右表(toy)為基準,來查詢所有玩具的擁有者姓名,對於沒有擁有者的為NULL

聯合:UNION, INTERSECT, EXCEPT

聯集查詢就是合并幾次的查詢結果到一張表裡

select user_id from user union select user_id from toy order by user_id;//聯合的兩個查詢語句的列必須相同, union可以串連多個, 假如排序在最後一條語句後面加order by;(重複自動去除)

union all:

select user_id from user union all select user_id from toy order by user_id;
//和union的運作方式一樣,就是union all的結果裡會返回所有值,而不是每個值的複製,包裹重複的也會列出來。

用union來建立表格:
create table m_union as select user_id from user union select user_id from toy;//這樣會把user裡的user_id和toy表裡的user_id的內容都拿出來放到心建立的m_union表格裡。

INTERSECT,EXCEPT

  • INTERSECT是交集.
  • EXCEPT是差集的意思

select user_id from user intersect select user_id from toy;
//就是返回這兩個select查詢相同的部分.(這裡就是有玩具的孩子的user_id)

select user_id from user except select user_id from toy;
//就是返回第一個select 減去第二個select 結果的部分。(這裡就是沒有玩具的孩子的user_id)

大致先這麼多,以後再說聯結轉換子查詢

聯繫我們

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