mysql 查询逻辑及查询顺序

 1、

https://jingyan.baidu.com/article/f3e34a12f05bfaf5ea65354a.htmlhttps://jingyan.baidu.com/article/f3e34a12f05bfaf5ea65354a.html

 2、

mysql全连接 - 星朝 - 博客园https://www.cnblogs.com/jpfss/p/11244720.html

 3、

数据库多表查询之 where和INNER JOIN_码农崛起-CSDN博客_inner join wherehttps://blog.csdn.net/Michaelwubo/article/details/80923248

4、

数据库SQL语句中 where,group by,having,order by的执行顺序_软件老王-CSDN博客https://blog.csdn.net/wjg8209/article/details/113934112

5、

当一个SQL语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序 - 雨姗 - 博客园https://www.cnblogs.com/shanshanlaichi/p/6533870.html

 6、

https://www.jb51.net/article/158100.htmhttps://www.jb51.net/article/158100.htm

 7、

数据库 - 标签 - 付大石 - 博客园https://www.cnblogs.com/fudashi/tag/%E6%95%B0%E6%8D%AE%E5%BA%93/

8、

MySQL数据库的主键和外键详解3 - 知乎https://zhuanlan.zhihu.com/p/114834741

9、

mysql where条件执行顺序_步步深入MySQL:架构->查询执行流程->SQL解析顺序_weixin_39612733的博客-CSDN博客https://blog.csdn.net/weixin_39612733/article/details/110844343

 10、

mysql where条件执行顺序_MySQL复杂where条件分析_weixin_39842682的博客-CSDN博客https://blog.csdn.net/weixin_39842682/article/details/110493301

11、

mysql 中sql的执行顺序 - 帅LOVE俊 - 博客园https://www.cnblogs.com/shuaiandjun/p/9490784.html

12、(重点)

SQL语句执行顺序 - 霞光里 - 博客园https://www.cnblogs.com/loong-hon/p/13470263.html

mysql中常见问题(where,group by,having顺序问题)

1,where,group by,having顺序问题:

顺序:where限制属性-->   group by分组-->  having筛选-->  order by排序-->    limit限制记录条数

where 先执行,然后group by 再执行,最后是 having;

2、

union和union all

MySQL UNION 语法

MySQL UNION 用于把来自多个 SELECT 语句的结果组合到一个结果集合中。语法为:

SELECT column,... FROM table1 
UNION [ALL]
SELECT column,... FROM table2
...

在多个 SELECT 语句中,对应的列应该具有相同的字段属性,且第一个 SELECT 语句中被使用的字段名称也被用于结果的字段名称。

UNION 与 UNION ALL 的区别

当使用 UNION 时,MySQL 会把结果集中重复的记录删掉,而使用 UNION ALL ,MySQL 会把所有的记录返回,且效率高于 UNION。

3、

一、sql执行顺序 
(1)from 
(3) join 
(2) on 
(4) where 
(5)group by
(6)having

(7) avg,sum....

(8) select 
(9) distinct 
(10) order by

(11)limit

4、

5、执行顺序,注意前面标注的序号:

# mysql 执行顺序,注意前面标注的序号:

(7)     SELECT 
(8)     DISTINCT <select_list>
(1)     FROM <left_table>
(3)     <join_type> JOIN <right_table>
(2)     ON <join_condition>
(4)     WHERE <where_condition>
(5)     GROUP BY <group_by_list>
(6)     HAVING <having_condition>
(9)     ORDER BY <order_by_condition>
(10)    LIMIT <limit_number>

6、举个例子:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for bj_table
-- ----------------------------
DROP TABLE IF EXISTS `bj_table`;
CREATE TABLE `bj_table`  (
  `xuehaoid` int(0) NOT NULL,
  `banji` int(0) NULL DEFAULT NULL,
  `chengji` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`xuehaoid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of bj_table
-- ----------------------------
INSERT INTO `bj_table` VALUES (1, 1, 86);
INSERT INTO `bj_table` VALUES (2, 1, 95);
INSERT INTO `bj_table` VALUES (3, 2, 89);
INSERT INTO `bj_table` VALUES (4, 1, 83);
INSERT INTO `bj_table` VALUES (5, 2, 86);
INSERT INTO `bj_table` VALUES (6, 3, 92);
INSERT INTO `bj_table` VALUES (7, 3, 86);
INSERT INTO `bj_table` VALUES (8, 1, 86);

SET FOREIGN_KEY_CHECKS = 1;


# -------------------------------- 上面是建表及数据

#--------------------------------- 下面是应用的例子

select banji,SUM(chengji) as cj_total
from bj_table
where banji < 6
GROUP BY banji
HAVING cj_total > 176
ORDER BY cj_total ASC
LIMIT 1,2;

7、单独放在sql语句后面,能查询出多少条数据。

      SELECT FOUND_ROWS();

猜你喜欢

转载自blog.csdn.net/weixin_54217632/article/details/120038753