联结表
作者:互联网
inner join 内联
-- 内联 查询在 student5中,也在student6中的数据。 SELECT student5.wechat_id from student5 -- 联结关系 表名 INNER JOIN student6 -- 联结条件 ON student5.wechat_id = student6.wechat_id
left join 左联结
这里有两个表,1个顾客表(custormer1107) 有两个字段
- c_no 顾客id
- c_name 顾客姓名
订单表(orders1107) 有三个字段
- o_c_no 顾客id
- order_money 订单金额
- o_no 订单号
CREATE TABLE custormer1107 ( c_no varchar(50) not null, c_name VARCHAR(50) not null ) INSERT INTO custormer1107 values("0003","wangwu"); SELECT * from custormer1107 CREATE table orders1107 ( o_c_no VARCHAR(50) not null, -- 顾客id order_money int not null, -- 订单金额 o_no VARCHAR(50) not null -- 订单号 ); SELECT * from orders1107
顾客表中有三个数据
订单表中存放跟顾客相关的订单数据
两个表中通过 顾客id进行关联。
现在要查询 所有顾客的订单信息,如果使用之前的条件查询
select custormer1107.c_name, custormer1107.c_no, orders1107.o_no,orders1107.order_money FROM custormer1107, orders1107 where custormer1107.c_no = orders1107.o_c_no
得出的结果只有
那些没有购买过商品的人员信息显示不出来。
如果想把所有人员信息都显示。可以使用 左联结-- 所有的用户的订单信息 select custormer1107.c_name, custormer1107.c_no, orders1107.o_no,orders1107.order_money FROM custormer1107 -- 左联 会把custormer1107 所有数据作为依据。 LEFT JOIN orders1107 -- 与 orders1107 表中数据进行关联 on custormer1107.c_no = orders1107.o_c_no
标签:no,--,联结,orders1107,custormer1107,顾客,id 来源: https://www.cnblogs.com/linho/p/15260993.html