Home >Database >Mysql Tutorial >最常用的几个mysql查询

最常用的几个mysql查询

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:27:351090browse

数据库查询非常普遍,下面写几个很常见的查询 1.SELECT chengji, class.name ,student.nameFROM greate,student,class whereclass.id=greate.classid and student.id=greate.id and chengji80; 第一个就是用到多表的查询。非常常见的一种查询 2.select max(

 

数据库查询非常普遍,下面写几个很常见的查询

1.SELECT chengji, class.name ,student.nameFROM greate,student,class  whereclass.id=greate.classid and student.id=greate.id and chengji>80;

第一个就是用到多表的查询。非常常见的一种查询

 

 

2.select max(chengji) ,class.name from greate , class where  class.id=greate.classid group by classid;

 

这是一个很常见的分组查询,where要放到grouprby前面,where一般是先过滤出你要查询的数据,然后在进行分组。

 

3.select * from greate where id in (selectid from student where id='000001');

 

这个是非常常见的子查询。子查询注意一点。In就是在结果集中选取元素。所以你select的语句查出的结果集一定要和in前面的字段对应,要不至少类型对应也可以。

 

4.比如说有20个学生要查找学习成绩是第七第八名的怎么办?

我们会想首先进行排序,取出成绩前八名的学生,接着将排序倒置,取前两名的。

要不可以先排序,取出前八名的,再取不在前六名的学生也可以。这是正常思路。

 

在mysql中可以用limit

 

select chengji from greate order by chengji asc limit 7,2;

limit意思是重第七个数开始取,共取2个数。所以7,8名很简单就取到了。

 

5.看上面第二实例其实施一个内连接,意思就是说,from后面跟着的是多个表

当然会还有外连接,比如说左外连接,右外连接,全外连接。

左外连接是指将左表中的所有数据分别于右表中的每条shuju进行连接组合,返回除内连接的数据外还包括左表中不符合条件的数据,并在右表的相应列中加null。

 

我写一个外连接的语法

Slecet 字段 from 表名1left|right  join 表名2. On  表名1.字段=表名2.字段;

 

6.Union 和union all

Union 可以将相同结果去掉,union all只是将结果拼接

 

7.正则表达式

Select * from student where name regexp ‘[ceo]’

意思是找name中包含ceo3个字母的任意一个的记录。

Select * from student where name regexp ‘a*c’

意思是找name中c之前出现a的记录。当然可以用‘+’不过+必须有一个字符

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn