0.基础表
| ```sql runoob_author runoob_count
baidu.com 100 qq.com 200 weixin.com 300
| ```sql
runoob_id runoob_title runoob_author
--------- ------------ --------------
1 php baidu.com
2 PHP baidu.com
3 Java qq.com
4 Python qq.com
5 C pass
| | —- | —- |
1.基础查询
基础查询```sql SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M]
<a name="vY1Xw"></a>
### 2.连接的使用
连接的使用
- **inner join(内连接or等值连接)**:获取两个表中字段匹配相同的记录。
- ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1668314817873-b5837d9c-fac1-433d-9401-a76d2266ba6d.png#averageHue=%23f8f8f8&clientId=uf666f6bb-13d8-4&from=paste&height=103&id=kFwZs&originHeight=351&originWidth=518&originalType=binary&ratio=1&rotation=0&showTitle=false&size=53088&status=done&style=none&taskId=u340263bd-d94e-47e5-91bf-3c2f9f4c9d0&title=&width=152.40000915527344)
- **left join(左连接):**获取左表所有记录,即使右表没有对应匹配的记录。
- ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1668314826299-f3b03151-0c9d-4f85-80e9-b1e9ddcdcc0c.png#averageHue=%23f9f9f9&clientId=uf666f6bb-13d8-4&from=paste&height=105&id=uKEn3&originHeight=344&originWidth=495&originalType=binary&ratio=1&rotation=0&showTitle=false&size=54043&status=done&style=none&taskId=u5672b53a-bc35-4014-9642-3eea2ed1c10&title=&width=151)
- **right join(右连接):** 获取右表所有记录,即使左表没有对应匹配的记录。
- ![image.png](https://cdn.nlark.com/yuque/0/2022/png/25474243/1668314836646-3a511d08-45c3-4b4e-b0b7-9fbc0babb829.png#averageHue=%23fafafa&clientId=uf666f6bb-13d8-4&from=paste&height=104&id=LuhGG&originHeight=340&originWidth=505&originalType=binary&ratio=1&rotation=0&showTitle=false&size=56007&status=done&style=none&taskId=u0e73e002-8930-45db-806d-e05cf1b3cc7&title=&width=154)
```sql
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a INNER JOIN tcount b ON a.runoob_author = b.runoob_author;
runoob_id runoob_author runoob_count
--------- ------------- --------------
1 baidu.com 100
2 baidu.com 100
3 qq.com 200
4 qq.com 200
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a LEFT JOIN tcount b ON a.runoob_author = b.runoob_author;
runoob_id runoob_author runoob_count
--------- ------------- --------------
1 baidu.com 100
2 baidu.com 100
3 qq.com 200
4 qq.com 200
5 pass (NULL)
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a RIGHT JOIN tcount b ON a.runoob_author = b.runoob_author;
runoob_id runoob_author runoob_count
--------- ------------- --------------
1 baidu.com 100
2 baidu.com 100
3 qq.com 200
4 qq.com 200
(NULL) (NULL) 300
-- mysql没有全连接 所以需要使用union 联合查询来实现:左连接 union 右连接
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a LEFT JOIN tcount b ON a.runoob_author = b.runoob_author
union
SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a RIGHT JOIN tcount b ON a.runoob_author = b.runoob_author;
runoob_id runoob_title runoob_author runoob_author runoob_count
--------- ------------ ------------- ------------- --------------
1 php baidu.com baidu.com 100
2 PHP baidu.com baidu.com 100
3 Java qq.com qq.com 200
4 Python qq.com qq.com 200
5 C pass (NULL) (NULL)
(NULL) (NULL) (NULL) weixin.com 300
3.子查询-where
WHEREwhere型子查询:把内层查询的结果作为外层查询的比较条件
SELECT
`runoob`.`runoob_id`, `runoob`.`runoob_author`, `runoob`.`runoob_title`, `tcount`.`runoob_count`
FROM
`runoob` RIGHT JOIN `tcount`
ON
`runoob`.`runoob_author`=`tcount`.`runoob_author`
WHERE
`tcount`.`runoob_count`=(SELECT MIN(`runoob_count`) FROM `tcount`)
runoob_id runoob_author runoob_title runoob_count
--------- ------------- ------------ --------------
1 baidu.com php 100
2 baidu.com PHP 100
4.子查询-from
FROM把内层的查询结果当成临时表,供外层sql再次查询。查询结果集可以当成表看待。临时表要使用一个别名。
SELECT goods_id,goods_name,cat_id,shop_price FROM
(SELECT goods_id,goods_name,cat_id,shop_price FROM goods ORDER BY cat_id ASC,goods_id DESC) AS tmp
GROUP BY cat_id;
5.exists型子查询
exists把外层sql的结果,拿到内层sql去测试,如果内层的sql成立,则该行取出。内层查询是exists后的查询。
-- exists子查询,如果exists后的内层查询能查出数据,则表示存在;为空则不存在。
SELECT `runoob`.`runoob_id`,`runoob`.`runoob_author`,`runoob`.`runoob_title`
FROM `runoob` WHERE EXISTS(
SELECT 1 FROM `tcount` WHERE `tcount`.`runoob_author`=`runoob`.`runoob_author`
)
runoob_id runoob_author runoob_title
--------- ------------- --------------
1 baidu.com php
2 baidu.com PHP
3 qq.com Java
4 qq.com Python
-- 使用 any 查出类别大于任何一个num值的类别,NY关键词必须后面接一个比较操作符。
-- ANY关键词的意思是“对于在子查询返回的列中的任一数值,如果比较结果为TRUE的话,则返回TRUE”。
SELECT cat_id,cat_name FROM category WHERE cat_id > ANY (SELECT num FROM nums);
-- 查询出来的结果包含在其中 与=any效果一致
SELECT cat_id,cat_name FROM category WHERE cat_id IN (SELECT num FROM nums);
-- 词语ALL必须接在一个比较操作符的后面。ALL的意思是“对于子查询返回的列中的所有值,如果比较 结果为TRUE,则返回TRUE。”
select cat_id.cat_name from category where cat_id > all(select num fronm nums)
-- 不在子查询的内容中,NOT IN不是<> ANY的别名,但是是<> ALL的别名
select cat_id.cat_name from category where cat_idwherecat_id not in(select num fronm nums)
6.limit介绍
limit``sql
Limit 初始位置,计数位
-- 初始位置: 表示从哪条记录开始显示
-- 记录数 : 表示显示记录的条数。
-- 第一条记录的位置是 0,第二条记录的位置是 1。
SELECT * FROM
runoob` LIMIT 2,3
输出:
runoob_id runoob_title runoob_author
3 Java qq.com
4 Python qq.com
5 C pass
```sql
Limit 记录数
-- 记录数: 表示显示记录的条数。
-- 如果“记录数”的值小于查询结果的总数,则会从第一条记录开始,显示指定条数的记录。
SELECT * FROM `runoob` LIMIT 2
输出:
runoob_id runoob_title runoob_author
--------- ------------ ---------------
1 php baidu.com
2 PHP baidu.com
-- 如果“记录数”的值大于查询结果的总数,则会直接显示查询出来的所有记录。
SELECT * FROM `runoob` LIMIT 9
输出:
runoob_id runoob_title runoob_author
--------- ------------ ---------------
1 php baidu.com
2 PHP baidu.com
3 Java qq.com
4 Python qq.com
5 C pass
Limi 记录数 Offset 初始位置
-- 初始位置: 指定从哪条记录开始显示
-- 记录数 : 表示显示记录的条数
SELECT * FROM `runoob` LIMIT 2 OFFSET 2
输出:
runoob_id runoob_title runoob_author
--------- ------------ ---------------
3 Java qq.com
4 Python qq.com