MySQL查询语法汇总

前言:

本篇文章主要简介下MySQL中where,group by ,order by ,limit,join,union ,union all,子表等查询语法。

测试数据准备

create table emp (  empno numeric(4) not null,  ename varchar(10),  job varchar(9),  mgr numeric(4),  hiredate datetime,  sal numeric(7, 2),  comm numeric(7, 2),  deptno numeric(2));
create table dept (  deptno numeric(2),  dname varchar(14),  loc varchar(13));
create table salgrade (  grade numeric,  losal numeric,  hisal numeric);
insert into dept values (10, 'ACCOUNTING', 'NEW YORK');insert into dept values (20, 'RESEARCH', 'DALLAS');insert into dept values (30, 'SALES', 'CHICAGO');insert into dept values (40, 'OPERATIONS', 'BOSTON');
insert into salgrade values (1, 700, 1200);insert into salgrade values (2, 1201, 1400);insert into salgrade values (3, 1401, 2000);insert into salgrade values (4, 2001, 3000);insert into salgrade values (5, 3001, 9999);
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);

1.模糊查询

mysql> select * from emp where ename like '%S%'; +-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7369 | SMITH | CLERK  | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |   20 || 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |   20 || 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000.00 | NULL |   20 || 7876 | ADAMS | CLERK  | 7788 | 1983-01-12 00:00:00 | 1100.00 | NULL |   20 || 7900 | JAMES | CLERK  | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL |   30 |+-------+-------+---------+------+---------------------+---------+------+--------+5 rows in set (0.00 sec)
mysql> select * from emp where ename like 'S%'; +-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7369 | SMITH | CLERK  | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL |   20 || 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 00:00:00 | 3000.00 | NULL |   20 |+-------+-------+---------+------+---------------------+---------+------+--------+2 rows in set (0.01 sec)
mysql> select * from emp where ename like '%S';+-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |   20 || 7876 | ADAMS | CLERK  | 7788 | 1983-01-12 00:00:00 | 1100.00 | NULL |   20 || 7900 | JAMES | CLERK  | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL |   30 |+-------+-------+---------+------+---------------------+---------+------+--------+3 rows in set (0.00 sec)
mysql> select * from emp where ename like '_O%';+-------+-------+---------+------+---------------------+---------+------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm | deptno |+-------+-------+---------+------+---------------------+---------+------+--------+| 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |   20 || 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |   20 |+-------+-------+---------+------+---------------------+---------+------+--------+2 rows in set (0.00 sec)

总结:%表示任意0个或多个字符,可匹配任意类型和长度的字符;_表示任意单个字符,匹配单个任意字符。

2.排序

mysql> select * from emp order by sal;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7369 | SMITH | CLERK   | 7902 | 1980-12-17 00:00:00 | 800.00 |  NULL |   20 || 7900 | JAMES | CLERK   | 7698 | 1981-12-03 00:00:00 | 950.00 |  NULL |   30 || 7876 | ADAMS | CLERK   | 7788 | 1983-01-12 00:00:00 | 1100.00 |  NULL |   20 || 7521 | WARD  | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 7934 | MILLER | CLERK   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  NULL |   10 || 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 |  0.00 |   30 || 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7782 | CLARK | MANAGER  | 7839 | 1981-06-09 00:00:00 | 2450.00 |  NULL |   10 || 7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 00:00:00 | 2850.00 |  NULL |   30 || 7566 | JONES | MANAGER  | 7839 | 1981-04-02 00:00:00 | 2975.00 |  NULL |   20 || 7788 | SCOTT | ANALYST  | 7566 | 1982-12-09 00:00:00 | 3000.00 |  NULL |   20 || 7902 | FORD  | ANALYST  | 7566 | 1981-12-03 00:00:00 | 3000.00 |  NULL |   20 || 7839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |  NULL |   10 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)
mysql> select * from emp order by sal asc;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7369 | SMITH | CLERK   | 7902 | 1980-12-17 00:00:00 | 800.00 |  NULL |   20 || 7900 | JAMES | CLERK   | 7698 | 1981-12-03 00:00:00 | 950.00 |  NULL |   30 || 7876 | ADAMS | CLERK   | 7788 | 1983-01-12 00:00:00 | 1100.00 |  NULL |   20 || 7521 | WARD  | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 7934 | MILLER | CLERK   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  NULL |   10 || 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 |  0.00 |   30 || 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7782 | CLARK | MANAGER  | 7839 | 1981-06-09 00:00:00 | 2450.00 |  NULL |   10 || 7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 00:00:00 | 2850.00 |  NULL |   30 || 7566 | JONES | MANAGER  | 7839 | 1981-04-02 00:00:00 | 2975.00 |  NULL |   20 || 7788 | SCOTT | ANALYST  | 7566 | 1982-12-09 00:00:00 | 3000.00 |  NULL |   20 || 7902 | FORD  | ANALYST  | 7566 | 1981-12-03 00:00:00 | 3000.00 |  NULL |   20 || 7839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |  NULL |   10 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)
mysql> select * from emp order by sal desc;+-------+--------+-----------+------+---------------------+---------+---------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm  | deptno |+-------+--------+-----------+------+---------------------+---------+---------+--------+| 7839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |  NULL |   10 || 7788 | SCOTT | ANALYST  | 7566 | 1982-12-09 00:00:00 | 3000.00 |  NULL |   20 || 7902 | FORD  | ANALYST  | 7566 | 1981-12-03 00:00:00 | 3000.00 |  NULL |   20 || 7566 | JONES | MANAGER  | 7839 | 1981-04-02 00:00:00 | 2975.00 |  NULL |   20 || 7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 00:00:00 | 2850.00 |  NULL |   30 || 7782 | CLARK | MANAGER  | 7839 | 1981-06-09 00:00:00 | 2450.00 |  NULL |   10 || 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 |  0.00 |   30 || 7934 | MILLER | CLERK   | 7782 | 1982-01-23 00:00:00 | 1300.00 |  NULL |   10 || 7521 | WARD  | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 || 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |   30 || 7876 | ADAMS | CLERK   | 7788 | 1983-01-12 00:00:00 | 1100.00 |  NULL |   20 || 7900 | JAMES | CLERK   | 7698 | 1981-12-03 00:00:00 | 950.00 |  NULL |   30 || 7369 | SMITH | CLERK   | 7902 | 1980-12-17 00:00:00 | 800.00 |  NULL |   20 |+-------+--------+-----------+------+---------------------+---------+---------+--------+14 rows in set (0.00 sec)

总结:order by排序默认按asc升序来排列也可指定desc降序排列

3.限制多少行

mysql> select * from emp limit 3;+-------+-------+----------+------+---------------------+---------+--------+--------+| empno | ename | job   | mgr | hiredate      | sal   | comm  | deptno |+-------+-------+----------+------+---------------------+---------+--------+--------+| 7369 | SMITH | CLERK  | 7902 | 1980-12-17 00:00:00 | 800.00 |  NULL |   20 || 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |   30 || 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 |   30 |+-------+-------+----------+------+---------------------+---------+--------+--------+3 rows in set (0.00 sec)
mysql> select * from emp order by sal desc limit 3;+-------+-------+-----------+------+---------------------+---------+------+--------+| empno | ename | job    | mgr | hiredate      | sal   | comm | deptno |+-------+-------+-----------+------+---------------------+---------+------+--------+| 7839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |   10 || 7902 | FORD | ANALYST  | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |   20 || 7788 | SCOTT | ANALYST  | 7566 | 1982-12-09 00:00:00 | 3000.00 | NULL |   20 |+-------+-------+-----------+------+---------------------+---------+------+--------+3 rows in set (0.00 sec)

总结:limit限定显示前多少行,可与order by联合使用

4.聚合函

count() sum()函数用法:#1.各个部门的薪水和mysql> select deptno,sum(sal) from emp group by deptno;+--------+----------+| deptno | sum(sal) |+--------+----------+|   10 | 8750.00 ||   20 | 10875.00 ||   30 | 9400.00 |+--------+----------+3 rows in set (0.01 sec)
#2.group by字段必须出现在select字段后面  各个部门的各个岗位的薪水和mysql> select deptno,job, sum(sal) from emp group by deptno ,job;+--------+-----------+----------+| deptno | job    | sum(sal) |+--------+-----------+----------+|   10 | CLERK   | 1300.00 ||   10 | MANAGER  | 2450.00 ||   10 | PRESIDENT | 5000.00 ||   20 | ANALYST  | 6000.00 ||   20 | CLERK   | 1900.00 ||   20 | MANAGER  | 2975.00 ||   30 | CLERK   |  950.00 ||   30 | MANAGER  | 2850.00 ||   30 | SALESMAN | 5600.00 |+--------+-----------+----------+9 rows in set (0.01 sec)
#3.having  薪水和>5000的各个部门的各个岗位mysql> select deptno,job, sum(sal)   -> from emp   -> group by deptno ,job  -> having sum(sal)>5000; +--------+----------+----------+| deptno | job   | sum(sal) |+--------+----------+----------+|   20 | ANALYST | 6000.00 ||   30 | SALESMAN | 5600.00 |+--------+----------+----------+2 rows in set (0.00 sec)#4.常用组合where order limit select deptno,job, sum(sal) as sum_salfrom emp where job='SALESMAN'group by deptno ,jobhaving sum(sal)>5000 order by sum(sal) desc limit 1;

下面介绍下join及union的用法
数据准备:

create table testa(aid int,aname varchar(40));
create table testb(bid int,bname varchar(40),age int);
insert into testa values(1,'xiaoming');insert into testa values(2,'LY');insert into testa values(3,'KUN');insert into testa values(4,'ZIDONG');insert into testa values(5,'HB');

insert into testb values(1,'xiaoming',10);insert into testb values(2,'LY',100);insert into testb values(3,'KUN',200);insert into testb values(4,'ZIDONG',110);insert into testb values(6,'niu',120);insert into testb values(7,'meng',130);insert into testb values(8,'mi',170);

5.left join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> left join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | LY    |  2 | LY    | 100 ||  3 | KUN   |  3 | KUN   | 200 ||  4 | ZIDONG  |  4 | ZIDONG  | 110 ||  5 | HB    | NULL | NULL   | NULL |+------+----------+------+----------+------+5 rows in set (0.00 sec)

总结:a left join b  a表全,用b表去匹配a表LEFT JOIN 关键字会从左表 (a) 那里返回所有的行,即使在右表 (b) 中没有匹配的行,匹配不到的列用NULL代替

6.right join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> right join testb as b on a.aid=b.bid;+------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | LY    |  2 | LY    | 100 ||  3 | KUN   |  3 | KUN   | 200 ||  4 | ZIDONG  |  4 | ZIDONG  | 110 || NULL | NULL   |  6 | niu   | 120 || NULL | NULL   |  7 | meng   | 130 || NULL | NULL   |  8 | mi    | 170 |+------+----------+------+----------+------+7 rows in set (0.00 sec)

总结:a right join b  b表全,用a表去匹配b表RIGHT JOIN 关键字会右表 (b) 那里返回所有的行,即使在左表 (a) 中没有匹配的行,匹配不到的列用NULL代替

7.inner join

mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> inner join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | LY    |  2 | LY    | 100 ||  3 | KUN   |  3 | KUN   | 200 ||  4 | ZIDONG  |  4 | ZIDONG  | 110 |+------+----------+------+----------+------+4 rows in set (0.00 sec)
mysql> select  -> a.aid,a.aname,  -> b.bid,b.bname,b.age  -> from testa as a  -> join testb as b on a.aid=b.bid; +------+----------+------+----------+------+| aid | aname  | bid | bname  | age |+------+----------+------+----------+------+|  1 | xiaoming |  1 | xiaoming |  10 ||  2 | LY    |  2 | LY    | 100 ||  3 | KUN   |  3 | KUN   | 200 ||  4 | ZIDONG  |  4 | ZIDONG  | 110 |+------+----------+------+----------+------+4 rows in set (0.00 sec)

总结:inner join 与join 效果一样在表中存在至少一个匹配时,INNER JOIN 关键字返回行

8.union与union all

mysql> select aid,aname from testa  -> union  -> select bid,bname from testb;+------+----------+| aid | aname  |+------+----------+|  1 | xiaoming ||  2 | LY    ||  3 | KUN   ||  4 | ZIDONG  ||  5 | HB    ||  6 | niu   ||  7 | meng   ||  8 | mi    |+------+----------+8 rows in set (0.01 sec)
mysql> select aid,aname from testa  -> union all  -> select bid,bname from testb;+------+----------+| aid | aname  |+------+----------+|  1 | xiaoming ||  2 | LY    ||  3 | KUN   ||  4 | ZIDONG  ||  5 | HB    ||  1 | xiaoming ||  2 | LY    ||  3 | KUN   ||  4 | ZIDONG  ||  6 | niu   ||  7 | meng   ||  8 | mi    |+------+----------+12 rows in set (0.00 sec)

总结:union 会去重 union all不去重

以上就是MySQL查询语法汇总的详细内容,更多关于MySQL查询语法的资料请关注我们其它相关文章!

(0)

相关推荐

  • MySQL数据库高级查询和多表查询

    MySQL多表查询 添加练习表 -- 用户表(user) CREATE TABLE `user`( `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户id(主键)', `username` VARCHAR(50) COMMENT '用户姓名', `age` CHAR(3) COMMENT '用户年龄' ); -- 订单表(orders) CREATE TABLE `orders`( `id` INT AUTO_INCREMENT PRIMARY KEY

  • MySQL中SQL分页查询的几种实现方法及优缺点

    [SQL]SQL分页查询总结 开发过程中经常遇到分页的需求,今天在此总结一下吧. 简单说来方法有两种,一种在源上控制,一种在端上控制.源上控制把分页逻辑放在SQL层:端上控制一次性获取所有数据,把分页逻辑放在UI上(如GridView).显然,端上控制开发难度低,适于小规模数据,但数据量增大时性能和IO消耗无法接受:源上控制在性能和开发难度上较为平衡,适应大多数业务场景:除此之外,还可以根据客观情况(性能要求,源与端的资源占用等)在源和端之间加一层,应用特殊算法和技术进行处理.以下主要讨论源上,

  • Mysql 查询数据库容量大小的方法步骤

    查询所有数据库的总大小 方法如下: mysql> use information_schema; mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES; +-----------+ | data | +-----------+ | 3052.76MB | +-----------+ 1 row in set (0.02 sec) 统计一下所有库数据量 每张表数据量=AVG_ROW_L

  • 浅谈mysql通配符进行模糊查询的实现方法

    在mysql数据库中,当我们需要模糊查询的时候 ,我们会使用到通配符. 首先我们来了解一下2个概念,一个是操作符,一个是通配符. 操作符 like就是SQL语句中的操作符,它的作用是指示在SQL语句后面的搜索模式是利用通配符而不是直接相等匹配进行比较. 注意:如果使用like操作符时没有使用通配符,那么效果是和等号是一致的. SELECT id,title FROM table WHERE title like '张三'; 这种写法就只能匹配张三的记录,而不能匹配像张三是个好人这样的记录. 通配

  • 一篇文章掌握MySQL的索引查询优化技巧

    前言 本文的内容是总结一些MySQL的常见使用技巧,以供没有DBA的团队参考.如无特殊说明,存储引擎以InnoDB为准. MySQL的特点 了解MySQL的特点有助于更好的使用MySQL,MySQL和其它常见数据库最大的不同在于存在存储引擎这个概念,存储引擎负责存储和读取数据.不同的存储引擎具有不同的特点,用户可以根据业务的特点选择适合的存储引擎,甚至是开发一个新的引擎.MySQL的逻辑架构大致如下: MySQL默认的存储引擎是InnoDB,该存储引擎的主要特点是: 支持事务处理 支持行级锁 数

  • mysql如何查询日期与时间

    前言: 在项目开发中,一些业务表字段经常使用日期和时间类型,而且后续还会牵涉到这类字段的查询.关于日期及时间的查询等各类需求也很多,本篇文章简单讲讲日期及时间字段的规范化查询方法. 1.日期和时间类型概览 MySQL支持的日期和时间类型有 DATETIME.TIMESTAMP.DATE.TIME.YEAR ,几种类型比较如下: 涉及到日期和时间字段类型选择时,根据存储需求选择合适的类型即可. 2.日期和时间相关函数 处理日期和时间字段的函数有很多,有的经常会在查询中使用到,下面介绍下几个相关函数

  • 浅谈pymysql查询语句中带有in时传递参数的问题

    直接给出例子说明: cs = conn.cursor() img_ids = [1,2,3] sql = "select img_url from img_url_table where id in %s" cs.execute(sql, (img_ids, )) # 直接传递元组包裹列表即可 补充知识:Python将多行数据处理成SQL语句中where条件in(' ',' ',' ')的数据 在工作中有时需要查询上万行指定的数据,就会用到SQL语句中 select * from ta

  • python pymysql链接数据库查询结果转为Dataframe实例

    我就废话不多说了,大家还是直接看代码吧! import pymysql import pandas as pd def con_sql(db,sql): # 创建连接 db = pymysql.connect(host='127.0.0.1', port=3308, user='name', passwd='password', db=db, charset='utf8') # 创建游标 cursor = db.cursor() cursor.execute(sql) result = curs

  • MySQL 多表关联一对多查询实现取最新一条数据的方法示例

    本文实例讲述了MySQL 多表关联一对多查询实现取最新一条数据的方法.分享给大家供大家参考,具体如下: MySQL 多表关联一对多查询取最新的一条数据 遇到的问题 多表关联一对多查询取最新的一条数据,数据出现重复 由于历史原因,表结构设计不合理:产品告诉我说需要导出客户信息数据,需要导出客户的 所属行业,纳税性质 数据:但是这两个字段却在订单表里面,每次客户下单都会要求客户填写:由此可知,客户数据和订单数据是一对多的关系:那这样的话,问题就来了,我到底以订单中的哪一条数据为准呢?经过协商后一致同

  • 浅谈MySQL使用笛卡尔积原理进行多表查询

    MySQL的多表查询(笛卡尔积原理) 先确定数据要用到哪些表. 将多个表先通过笛卡尔积变成一个表. 然后去除不符合逻辑的数据(根据两个表的关系去掉). 最后当做是一个虚拟表一样来加上条件即可. 注意:列名最好使用表别名来区别. 笛卡尔积 Demo: 左,右连接,内,外连接 l 内连接: 要点:返回的是所有匹配的记录. select * from a,b where a.x = b.x ////内连接 l 外连接有左连接和右连接两种. 要点:返回的是所有匹配的记录 外加 每行主表外键值为null的

随机推荐