跟王老師學MySQL: 基本查詢語句

來源:互聯網
上載者:User

標籤:mysql;mysql資料類型

跟王老師學MySQL: 基本查詢語句
主講教師:王少華   QQ群號:483773664學習內容

查詢語句的基本文法


查詢資料指從資料庫中擷取所需要的資料。MySQL中是使用SELECT語句來查詢資料的

一、select的基本文法形式如下

1

2

3

4

5

SELECT  屬性列表 

FROM  表名

[ WHERE  條件運算式1 ]

[ GROUP BY  屬性名稱1  [ HAVING 條件運算式2 ] ]

[ ORDER BY  屬性名稱2  [ ASC | DESC ] ]

屬性列表:需要查詢的欄位名

表名:需要查詢的表,可以有多個

條件運算式1:指定查詢條件

屬性名稱1:按該欄位進行分組

條件運算式2:滿足該運算式的資料才能輸出

屬性名稱2:按該欄位中的資料百行排序

ASC:升序

DESC:降序

二 舉例
(一)準備資料

1

2

3

4

5

6

7

8

9

10

11

12

13

14

create table employee(

   num int primary key auto_increment,

   name varchar(20),

   age int,

   sex varchar(4),

   homeaddr varchar(50)

);

insert into employee(name,age,sex,homeaddr)values(‘張三‘,26,‘男‘,‘浙江杭州‘);

insert into employee(name,age,sex,homeaddr)values(‘李四‘,24,‘女‘,‘浙江寧波‘);

insert into employee(name,age,sex,homeaddr)values(‘王五‘,29,‘男‘,‘浙江台州‘);

insert into employee(name,age,sex,homeaddr)values(‘趙六‘,21,‘男‘,‘浙江湖州‘);

insert into employee(name,age,sex,homeaddr)values(‘孫悟空‘,21,‘男‘,‘浙江寧波‘);

insert into employee(name,age,sex,homeaddr)values(‘豬八戒‘,22,‘男‘,‘浙江寧波‘);

insert into employee(name,age)values(‘唐僧‘,21);


(二)簡單查詢

1

select num,name,age,sex,homeaddr from employee;

650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M02/83/E3/wKiom1d_MN3BOpdrAAAY_zRB17Q351.png" alt="wKiom1d_MN3BOpdrAAAY_zRB17Q351.png" />

(三)含有where子句和order by 子句

1

select num,name,age,sex,homeaddr from employee where age<26 order by num DESC;

650) this.width=650;" border="0" src="http://s3.51cto.com/wyfs02/M00/83/E2/wKioL1d_MN7wMgtRAAAa9WakBYE985.png" alt="wKioL1d_MN7wMgtRAAAa9WakBYE985.png" />





本文出自 “跟王老師學編程” 部落格,請務必保留此出處http://teacherwang.blog.51cto.com/10946447/1812576

跟王老師學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.