【PHP & MySQL】数据库专题 第七课 数据查询语句
概述
从今天开始, 小白我将带领大家一起来补充一下 数据库的知识.
ORDER BY
在 SQL 中, 我们可以使用 ORDER BY 对查询结果进行一列或多列排序.
SQL 语句:
SELECT 列名1, 列名2 FROM 表名1, 表名2
ORDER BY 列名, 列名[ASC|DESC]
- 1
- 2
注意:
- ASC 表示按升序排列, DESC 表示按降序排列
- 默认情况下对列按升序排序
表记录:
例 1
工资从低到高排序:
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句, 条件查询
$SQL = "SELECT * FROM user
ORDER BY salary";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[id] => 3
[username] => 王富贵
[password] => 123456
[sex] => 1
[salary] => 1000.00
)
Array
(
[id] => 4
[username] => 李大爷
[password] => 123123
[sex] => 1
[salary] => 1500.00
)
Array
(
[id] => 2
[username] => 张美丽
[password] => 123456
[sex] => 2
[salary] => 5000.00
)
Array
(
[id] => 5
[username] => 王二狗
[password] => 123123
[sex] => 1
[salary] => 6000.00
)
Array
(
[id] => 1
[username] => 我是小白呀
[password] => 123456
[sex] => 2
[salary] => 30000.00
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
例 2
工资从高到低排序:
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句, 条件查询
$SQL = "SELECT * FROM user
ORDER BY salary DESC";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
LIMIT
我们可以在 SELECT 语句中使用 LIMIT 来约束要返回的记录数, 通常使用 LIMIT 实现分页.
SQL 语句:
SELECT 列名1, 列名2 FROM 表1, 表2 LIMIT [偏移量] 行数
- 1
注意:
- 第一行的偏移量是 0, 而不是 1
- 行数为返回最大的行数
例 1
取前两个数据:
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT * FROM user
LIMIT 2";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[id] => 1
[username] => 我是小白呀
[password] => 123456
[sex] => 2
[salary] => 30000.00
)
Array
(
[id] => 2
[username] => 张美丽
[password] => 123456
[sex] => 2
[salary] => 5000.00
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
例 2
取第 2, 3 条数据:
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT * FROM user
LIMIT 1, 2";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[id] => 2
[username] => 张美丽
[password] => 123456
[sex] => 2
[salary] => 5000.00
)
Array
(
[id] => 3
[username] => 王富贵
[password] => 123456
[sex] => 1
[salary] => 1000.00
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
GROUP BY
从字面上理解, GROUP BY 表示根据某种规则对数据进行分组. 它必须配合聚合函数进行使用, 对数据进行分组后可以进行 COUNT, SUM, AVG, MAX 和 MIN 等运算.
SQL 语句:
SELECT 列名, 聚合函数(列名)
FROM 表名
GROUP BY 列名
- 1
- 2
- 3
注意:
- GROUP BY 可以对一列或多列进行分组
表记录:
例 1
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT gender, count(*) FROM student
GROUP BY gender";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[gender] => 1
[count(*)] => 3
)
Array
(
[gender] => 2
[count(*)] => 6
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
例 2
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT grade, count(*) FROM student
GROUP BY grade";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[grade] => 高三
[count(*)] => 2
)
Array
(
[grade] => 高一
[count(*)] => 4
)
Array
(
[grade] => 高二
[count(*)] => 3
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
HAVING
HAVING 可以解决 WHERE 关键词无法与聚合函数一起使用的问题. HAVING 可以对分组后的各组数据进行筛选.
SQL 语句:
SELECT 列名, 聚合函数(列名)
FROM 表名
GROUP BY 列名
HAVING 聚合函数(列名) 条件
- 1
- 2
- 3
- 4
例 1
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT grade, count(*) FROM student
GROUP BY grade
HAVING count(*) > 2";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[grade] => 高一
[count(*)] => 4
)
Array
(
[grade] => 高二
[count(*)] => 3
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
例 2
<?php
# 创建连接
$conn = mysqli_connect("localhost", "root", "admin", "study");
# 查看是否连接成功
if ($conn) {
echo "服务器连接成功!\n";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT gender, count(*) FROM student
GROUP BY gender
HAVING count(*) > 3";
# 执行SQL语句
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL语句执行成功!\n";
} else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
输出结果:
服务器连接成功!
SQL语句执行成功!
Array
(
[gender] => 2
[count(*)] => 6
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
GROUP_CONCAT
当我们使用GROUP BY
可以获取分组, 但是无法看到具体信息. 这时我们就可以使用GROUP_CONCAT
来获取具体信息. GROUP_CONCAT
配合GROUP BY
一起使用, 可以将一列的值按照指定分隔符进行拼接 (默认为 “,”).
SQL 语句:
GROUP_CONCAT(列名[排序列 ASC/DESC] [分隔符])
- 1
例 1
<?php
$conn = mysqli_connect("localhost", "root","admin","study");
if ($conn) {
echo "数据库链接成功";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT grade, count(*), GROUP_CONCAT(student_name)
FROM student
GROUP BY grade";
# 执行
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL 语句执行成功!";
}else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
输出结果:
数据库链接成功SQL 语句执行成功!Array
(
[grade] => 高一
[count(*)] => 4
[GROUP_CONCAT(student_name)] => 王富贵,杨美丽,杨美丽,杨美丽
)
Array
(
[grade] => 高三
[count(*)] => 2
[GROUP_CONCAT(student_name)] => 杨美丽,杨美丽
)
Array
(
[grade] => 高二
[count(*)] => 3
[GROUP_CONCAT(student_name)] => 杨美丽,杨美丽,赵有才
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
例 2
<?php
$conn = mysqli_connect("localhost", "root","admin","study");
if ($conn) {
echo "数据库链接成功";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT gender, count(*), GROUP_CONCAT(student_name)
FROM student
GROUP BY gender";
# 执行
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL 语句执行成功!";
}else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
输出结果:
数据库链接成功SQL 语句执行成功!Array
(
[gender] => 1
[count(*)] => 3
[GROUP_CONCAT(student_name)] => 杨美丽,杨美丽,杨美丽
)
Array
(
[gender] => 2
[count(*)] => 6
[GROUP_CONCAT(student_name)] => 王富贵,杨美丽,杨美丽,杨美丽,杨美丽,赵有才
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
DISTINCT
DISTINCT
用于在查询中返回父唯一不同值, 支持单列或多列. 在实际的应用中, 表中的某一列含有重复值是很常见的. 如果在查询数据时, 希望得到某列的所有不同值, 可以使用 DISTINCT
.
SQL 语句:
SELECT DISTINCT 列名1, 列名2 FROM 表名
- 1
例 1
<?php
$conn = mysqli_connect("localhost", "root","admin","study");
if ($conn) {
echo "数据库链接成功";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT DISTINCT grade FROM student";
# 执行
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL 语句执行成功!";
}else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
输出结果:
数据库链接成功SQL 语句执行成功!Array
(
[grade] => 高三
)
Array
(
[grade] => 高一
)
Array
(
[grade] => 高二
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
例 2
<?php
$conn = mysqli_connect("localhost", "root","admin","study");
if ($conn) {
echo "数据库链接成功";
} else {
echo mysqli_connect_error();
}
# SQL语句
$SQL = "SELECT DISTINCT gender FROM student";
# 执行
$result = mysqli_query($conn, $SQL);
# 查看是否执行成功
if ($result) {
echo "SQL 语句执行成功!";
}else {
echo mysqli_error($conn);
}
# 调试输出
while ($line = mysqli_fetch_assoc($result)) {
print_r($line);
}
# 关闭连接
mysqli_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
输出结果:
数据库链接成功SQL 语句执行成功!Array
(
[gender] => 1
)
Array
(
[gender] => 2
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
文章来源: iamarookie.blog.csdn.net,作者:我是小白呀,版权归原作者所有,如需转载,请联系作者。
原文链接:iamarookie.blog.csdn.net/article/details/122402960
- 点赞
- 收藏
- 关注作者
评论(0)