Learn MySQL with teacher Wang: Basic query statement
Teacher: Wang Shaohua QQ Group No.: 483773664 Learning Content
Basic syntax for query statements
Query data refers to getting the required data from the database. In MySQL, you use the SELECT statement to query the data
The basic grammatical form of a select is as follows
1 2 3 4 5 |
SELECT 属性列表
FROM 表名
[ WHERE 条件表达式1 ]
[ GROUP BY 属性名1 [ HAVING 条件表达式2 ] ]
[ ORDER BY 属性名2 [ ASC | DESC ] ]
|
Property List: The name of the field to query
Table name: The table you want to query, you can have more than one
Conditional expression 1: Specifying query criteria
Property name 1: Group By this field
Conditional expression 2: data that satisfies the expression can be output
Property Name 2: Sort by the data in this field by hundred rows
ASC: Ascending
DESC: Descending
Two examples
(i) Preparation of data
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);
|
(b) Simple Enquiry
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"/>
(iii) contains a WHERE clause and an ORDER BY clause
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"/>
This article is from "Learn programming with Mr. Wang" blog, please be sure to keep this source http://teacherwang.blog.51cto.com/10946447/1812576
Learn MySQL with teacher Wang: Basic query statement