MySQL 左连接出现重复记录的问题

今天遇到了一个 在 MySQL 左连接时 出现重复记录的问题 下面就是这个问题的说明 举个例子两个表主表为A 从表为B


A

aidplace
1大连
2上海
3北京

B

bidaidtypename
11学生
21老师
32领导
41学生
52老师

下面我想查询type为学生的A表和B表的所有信息

select * from A join B on a.aid=b.aid where B.type="学生"; 得到的结果是:

如果我查询type为学生的A表信息

select a.* from A join B on a.aid=b.aid where B.type="学生";

得到的结果为:

所以!!!就是所谓的重复,

如果说你想查找 type 为学生的都来自于哪个 place 可以直接 distinct ,例如:

select distinct a.* from A join B on a.aid=b.aid where B.type="学生";

得到的结果为:

但是就像上文提到的如果我查询type为学生的A表信息

所得到的两个一样的数据其中包含的意义其实是不一样的。


Hello, I am Mingyue Li. Read my thoughts on IT and Life

Copyright Declaration:

All articles and pictures contained on this web site is copyright protected. If reproduced, must contain this statement, and indicate the original author and the original address of this article