亲宝软件园·资讯

展开

mysql完整的select语句 mysql学习笔记之完整的select语句用法实例详解

随风行云 人气:0

本文实例讲述了mysql学习笔记之完整的select语句用法。分享给大家供大家参考,具体如下:

本文内容:

首发日期:2018-04-11


完整语法:

先给一下完整的语法,后面将逐一来讲解。

基础语法:select 字段列表 from 数据源;

完整语法:select 去重选项 字段列表 [as 字段别名] from 数据源 [where子句] [group by 子句] [having子句] [order by 子句] [limit子句];


去重选项::

示例:

去重前:image ,去重后image

create table student(name varchar(15),gender varchar(15));
insert into student(name,gender) values("lilei","male");
insert into student(name,gender) values("lilei","male");
select * from student;
select distinct * from student;

补充:


字段别名:

示例:

使用前:image,使用后image

create table student(name varchar(15),gender varchar(15));
insert into student(name,gender) values("lilei","male");
insert into student(name,gender) values("lilei","male");
select * from student;
select name as "姓名",gender as "性别" from student;

数据源:


where子句:

where几种语法:

补充:

select name as n ,gender from student where name ="lilei";
-- select name as n ,gender from student where n ="lilei"; --报错
select name as n ,gender from student having n ="lilei";

group by 子句:

-- 示例
select name,gender,count(name) as "组员" from student as d group by name;
select name,gender,count(name) as "组员" from student as d group by name,gender;

补充:


having子句:

-- 示例
select name as n ,gender from student having n ="lilei";
select name,gender,count(*) as "组员" from student as d group by name,gender having count(*) >2 ;-- 这里只显示记录数>2的分组

order by 子句:

-- 示例
select * from student order by name;
select * from student order by name,gender;
select * from student order by name asc,gender desc;

limit子句:

-- 示例
select * from student limit 1;
select * from student limit 3,1;
select * from student where name ="lilei" limit 1;
select * from student where name ="lilei" limit 3,1;

希望本文所述对大家MySQL数据库计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论