Heim > Datenbank > MySQL-Tutorial > Hauptteil

sql数据库查询语句的基本语法是什么

醉折花枝作酒筹
Freigeben: 2023-01-07 11:45:55
Original
13063 人浏览过

sql数据库查询语句的完整语法是“Select [select选项] 字段列表[字段别名]/* from 数据源 [where 字句] [group by子句 ][having 子句][order by 子句][limit 子句];”。

sql数据库查询语句的基本语法是什么

本教程操作环境:windows7系统、mysql8版、Dell G3电脑。

数据库是mysql,使用的数据库表名称是my_student.

表的完整数据信息是:

完整语法是:

Select [select选项] 字段列表[字段别名]/* from 数据源 [where 字句] [group by子句 ][having 子句][order by 子句][limit 子句];

①[select选项]:

Select 选项包含:ALL(所有,默认)、distinct(去重)。其中distinct针对的是查询结果的整条记录而言的。

select DISTINCT(sex) from my_student;

select DISTINCT(sex),name from my_student; 和 selectDISTINCT sex,name from my_student;结果是一样的。

②[where 字句]:where是唯一一个从磁盘开始拿数据的时候就开始进行判断的条件,从磁盘取出一条记录,开始进行where判断,判断结果如果成立,那么取出结果保存到内存,否则放弃。

select * from my_student where name = '哈哈1';

③[group by 子句 ]:分组子句,group by子句主要的作用是分组,从而进行统计操作,而不是为了展示(展示的时候,只会展示分组记录的第一条记录),分组时,一般会结合使用count()、max()、min()、avg()、sum()函数。

A、单子段分组:

selectc_id,count(*),max(height),min(height),avg(height),sum(age) from my_studentgroup by c_id ;

sql语句的意思是:my_student表以c_id进行分组,然后显示分组后的每组的c_id名称、每组的总数、每组的最高、最低、平均身高和每组的年龄总和。

B、多字段分组

select c_id,sex,count(*),max(height),min(height),avg(height),sum(age)from my_student group by c_id ,sex;

表示的含义是,对整个表先按照c_id进行分组,然后在此分组的基础之上,然后每组再按照sex,进行分组。

C、多字段分组(加上显示每组的某一字段的所有数据)

selectc_id,sex,count(*),max(height),min(height),avg(height),sum(age) ,GROUP_CONCAT(name)from my_student group by c_id ,sex;

④[having 子句]:having的作用类同where,而且having能做几乎所有where能做的事情,而where却不能做having能做的很多事情,主要是因为

where只能在磁盘提取数据的时候对数据进行操作;而在内存中对数据进行group by分组之后的结果进行处理,只能通过having。

selectc_id,count(*),max(height),min(height),avg(height),sum(age) from my_studentgroup by c_id having COUNT(*) >= 3;

⑤[order by 子句]:对数据进行排序操作,根据某个字段进行升序或者降序排序。(进行多字段排序的时候,先根据某一字段进行潘旭,然后在排序好的内部再按照某字段进行排序)

A、单个字段的排序:

select * from my_student order by c_id;

B、多字段排序

select * from my_student order by c_id,sex;

⑥[limit 子句]:限制结果的数量。Limit 偏移量  记录条数;

A、select * frommy_student limit 2;

B、select * frommy_student limit 0,3;

相关推荐:《mysql教程

以上是sql数据库查询语句的基本语法是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!