0.基础表

| ```sql runoob_author runoob_count


baidu.com 100 qq.com 200 weixin.com 300

  1. | ```sql
  2. runoob_id runoob_title runoob_author
  3. --------- ------------ --------------
  4. 1 php baidu.com
  5. 2 PHP baidu.com
  6. 3 Java qq.com
  7. 4 Python qq.com
  8. 5 C pass

| | —- | —- |

1.基础查询

基础查询```sql SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M]

  1. <a name="vY1Xw"></a>
  2. ### 2.连接的使用
  3. 连接的使用
  4. - **inner join(内连接or等值连接)**:获取两个表中字段匹配相同的记录。
  5. - ![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)
  6. - **left join(左连接):**获取左表所有记录,即使右表没有对应匹配的记录。
  7. - ![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)
  8. - **right join(右连接):** 获取右表所有记录,即使左表没有对应匹配的记录。
  9. - ![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)
  10. ```sql
  11. SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a INNER JOIN tcount b ON a.runoob_author = b.runoob_author;
  1. runoob_id runoob_author runoob_count
  2. --------- ------------- --------------
  3. 1 baidu.com 100
  4. 2 baidu.com 100
  5. 3 qq.com 200
  6. 4 qq.com 200
  1. SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a LEFT JOIN tcount b ON a.runoob_author = b.runoob_author;
  1. runoob_id runoob_author runoob_count
  2. --------- ------------- --------------
  3. 1 baidu.com 100
  4. 2 baidu.com 100
  5. 3 qq.com 200
  6. 4 qq.com 200
  7. 5 pass (NULL)
  1. SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a RIGHT JOIN tcount b ON a.runoob_author = b.runoob_author;
  1. runoob_id runoob_author runoob_count
  2. --------- ------------- --------------
  3. 1 baidu.com 100
  4. 2 baidu.com 100
  5. 3 qq.com 200
  6. 4 qq.com 200
  7. (NULL) (NULL) 300
  1. -- mysql没有全连接 所以需要使用union 联合查询来实现:左连接 union 右连接
  2. SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a LEFT JOIN tcount b ON a.runoob_author = b.runoob_author
  3. union
  4. SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob a RIGHT JOIN tcount b ON a.runoob_author = b.runoob_author;
  1. runoob_id runoob_title runoob_author runoob_author runoob_count
  2. --------- ------------ ------------- ------------- --------------
  3. 1 php baidu.com baidu.com 100
  4. 2 PHP baidu.com baidu.com 100
  5. 3 Java qq.com qq.com 200
  6. 4 Python qq.com qq.com 200
  7. 5 C pass (NULL) (NULL)
  8. (NULL) (NULL) (NULL) weixin.com 300

3.子查询-where

WHEREwhere型子查询:把内层查询的结果作为外层查询的比较条件

  1. SELECT
  2. `runoob`.`runoob_id`, `runoob`.`runoob_author`, `runoob`.`runoob_title`, `tcount`.`runoob_count`
  3. FROM
  4. `runoob` RIGHT JOIN `tcount`
  5. ON
  6. `runoob`.`runoob_author`=`tcount`.`runoob_author`
  7. WHERE
  8. `tcount`.`runoob_count`=(SELECT MIN(`runoob_count`) FROM `tcount`)
  1. runoob_id runoob_author runoob_title runoob_count
  2. --------- ------------- ------------ --------------
  3. 1 baidu.com php 100
  4. 2 baidu.com PHP 100

4.子查询-from

FROM把内层的查询结果当成临时表,供外层sql再次查询。查询结果集可以当成表看待。临时表要使用一个别名。

  1. SELECT goods_id,goods_name,cat_id,shop_price FROM
  2. (SELECT goods_id,goods_name,cat_id,shop_price FROM goods ORDER BY cat_id ASC,goods_id DESC) AS tmp
  3. GROUP BY cat_id;

5.exists型子查询

exists把外层sql的结果,拿到内层sql去测试,如果内层的sql成立,则该行取出。内层查询是exists后的查询。

  1. -- exists子查询,如果exists后的内层查询能查出数据,则表示存在;为空则不存在。
  2. SELECT `runoob`.`runoob_id`,`runoob`.`runoob_author`,`runoob`.`runoob_title`
  3. FROM `runoob` WHERE EXISTS(
  4. SELECT 1 FROM `tcount` WHERE `tcount`.`runoob_author`=`runoob`.`runoob_author`
  5. )
  1. runoob_id runoob_author runoob_title
  2. --------- ------------- --------------
  3. 1 baidu.com php
  4. 2 baidu.com PHP
  5. 3 qq.com Java
  6. 4 qq.com Python
  1. -- 使用 any 查出类别大于任何一个num值的类别,NY关键词必须后面接一个比较操作符。
  2. -- ANY关键词的意思是“对于在子查询返回的列中的任一数值,如果比较结果为TRUE的话,则返回TRUE”。
  3. SELECT cat_id,cat_name FROM category WHERE cat_id > ANY (SELECT num FROM nums);

image.png

  1. -- 查询出来的结果包含在其中 与=any效果一致
  2. SELECT cat_id,cat_name FROM category WHERE cat_id IN (SELECT num FROM nums);

image.png

  1. -- 词语ALL必须接在一个比较操作符的后面。ALL的意思是“对于子查询返回的列中的所有值,如果比较 结果为TRUE,则返回TRUE。”
  2. select cat_id.cat_name from category where cat_id > all(select num fronm nums)

image.png

  1. -- 不在子查询的内容中,NOT IN不是<> ANY的别名,但是是<> ALL的别名
  2. select cat_id.cat_name from category where cat_idwherecat_id not in(select num fronm nums)

image.png

6.limit介绍

limit``sql Limit 初始位置,计数位 -- 初始位置: 表示从哪条记录开始显示 -- 记录数 : 表示显示记录的条数。 -- 第一条记录的位置是 0,第二条记录的位置是 1。 SELECT * FROMrunoob` LIMIT 2,3 输出: runoob_id runoob_title runoob_author


  1. 3 Java qq.com
  2. 4 Python qq.com
  3. 5 C pass
  1. ```sql
  2. Limit 记录数
  3. -- 记录数: 表示显示记录的条数。
  4. -- 如果“记录数”的值小于查询结果的总数,则会从第一条记录开始,显示指定条数的记录。
  5. SELECT * FROM `runoob` LIMIT 2
  6. 输出:
  7. runoob_id runoob_title runoob_author
  8. --------- ------------ ---------------
  9. 1 php baidu.com
  10. 2 PHP baidu.com
  11. -- 如果“记录数”的值大于查询结果的总数,则会直接显示查询出来的所有记录。
  12. SELECT * FROM `runoob` LIMIT 9
  13. 输出:
  14. runoob_id runoob_title runoob_author
  15. --------- ------------ ---------------
  16. 1 php baidu.com
  17. 2 PHP baidu.com
  18. 3 Java qq.com
  19. 4 Python qq.com
  20. 5 C pass
  1. Limi 记录数 Offset 初始位置
  2. -- 初始位置: 指定从哪条记录开始显示
  3. -- 记录数 : 表示显示记录的条数
  4. SELECT * FROM `runoob` LIMIT 2 OFFSET 2
  5. 输出:
  6. runoob_id runoob_title runoob_author
  7. --------- ------------ ---------------
  8. 3 Java qq.com
  9. 4 Python qq.com