MySQL查询语句们
查询语句的基本格式:
select 列名1,列名2 from 表名
[where where_definition]
[group by {unsigned_integer | col_name | formula} [ASC | DESC], ...]
[having where_definition]
[order by {unsigned_integer | col_name | formula} [| DESC] ,...]
[limit [offset,] rows | rows OFFSET offset] ASC
说明:
1、查旬所有列:可用*来表示: select * from news_table;
2、[]中的为可选语句。 where 条件,用来筛选数据。
group by 用来分类汇总。
having :用来筛选分类汇总的结果。
order by用来排序。
limit用来取出指定的记录数。
根据sql查询表的个数,可以分为单表查询和多表查询。
单表查询
1、创建stu_info表
create table stu_info
(
id int auto_increment,
stu_num varchar(20),
stu_name varchar(20),
stu_sex char(2),
stu_age int,
stu_grade varchar(20),
stu_class varchar(20),
stu_subject varchar(20),
stu_fee decimal(6,2),
stu_time datetime,
primary key(id)
);
字段说明如下:
id,学号,姓名,性别,年龄,年级,班级,科目,成绩,时间
Code:code/
2、查询所有数据
格式:
select * from 表名;
例子:
Select * from stu_info;
查询这个表的所有列(字段)
Select id,stu_name from stu_info;
查询这个表的所有数据,只显示id和stu_name两个列。
指定别名:
select id as ‘序号’ from stu_info;
select id ‘序号’ from stu_info;
select id as ‘序号’,stu_name as ‘姓名’ from stu_info;
指定别名,只是为了显示更加真观,并不真正改变表的列名。
3、where语句
where语句,后面可以跟着多个条件,从而来限制查询的数据。多个条件之间,可以用 and 或者 or 来链接。
例如:
Select * from stu_info where id=5;
Select * from stu_info where id>5;
Select * from stu_info where id<=5;
Select * from stu_info wehre id<>5;
分别表示: id为5的数据;id大于5的数据;Id小于或者等于5的数据;id不等于的数据;
Select * from stu_info where id>2 and stu_name=‘张三’;
Select * from stu_info where id>2 or stu_name<>’张三’;
用括号来指定条件执行的先后顺序:
select * from stu_info where stu_grade=‘高一’ and (stu_class=‘一班’ or stu_class=‘二班’);
4、order by语句
order by 用来指定数据的排序方式。有升序和降序两种。desc表示降序,asc为升序,默认为升序,asc可省略。
例如:
select * from stu_info order by id asc; 按照id升序排序,其中asc可省略。
select * from stu_info order by id desc; 按照id降序
select * from stu_info where id<=5 order by stu_name; 按姓名升序
select * from stu_info where id<=5 order by stu_time desc; 按时间降序
Order by 后台可指定多个排序字段,中间以逗号分隔。
例如:
select * from stu_info order by stu_sex asc,stu_fee desc;
表示先按stu_sex(学
mysql 3 来自淘豆网m.daumloan.com转载请标明出处.