--高级查询在数据库中用得是最频繁的,也是应用最广泛的。 Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select distinct sex from student; --count 统计 select count(*) from student; select count(sex) from student; select count(dis
MySql数据库中的子查询:
子查询:在一条select查询语句中嵌套另一条select语句,其主要作用是充当查询条件或确定数据源。
代码案例如下:
例1. 查询大于平均年龄的学生:
select * from students where age > (select avg(age) from students);
例2. 查询学生在班的所有班级名字:
select name from classes where id in (select cls_id from students
1.3 子查询
语法:select * from 表1 where (子查询)
外面的查询称为父查询
子查询为父查询提供查询条件
1.3.1 标量子查询
特点:子查询返回的值是一个
-- 查找笔试成绩是80的学生
mysql> select * from stuinfo where stuno=(select stuno from stumarks where writtenexam=80);
+--------+---------+--------+--------+--------
SQL 中的 SELECT 命令不仅能够直接用来查询数据库中的数据,还能够嵌入到 WHERE 中作为子查询语句,或者将查询结果插入到另一个表中。
将子查询用于 WHERE 子句
子查询能够与 SELECT、INSERT、UPDATE 和 DELETE 语句一起使用。
下面以 SELECT 语句作为示例:
SELECT id, name
FROM table1
WHERE id in (
SELECT id
FROM table2);
在上面所给的例子中,我们将子
子查询像使用普通的表一样,被当作结果集的查询语句被称为子查询。所有可以使用表的地方几乎都可以使用子查询来代替。
SELECT * FROM (SELECT * FROM student where sAge<30 t
只有返回且仅返回一行、一列数据的子查询才能当成单值子查询。
子查询返回的值不止一个。当子查询跟随在=、! u3001=之后,或子查询用作表达式时,这种情况是不允许的。
tips:
select * from TblSt