mysql数据库里只有一条数据为啥查询出来有两条重复的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库里只有一条数据为啥查询出来有两条重复的数据相关的知识,希望对你有一定的参考价值。

mysql数据库里只有一条数据为什么查询出来有两条重复的数据 而且两条数据的主键都是一样的。
下面是查询的sql:

select a.*,a.prepared_date-now() prepared_date_flag,a.released_date-now() released_date_flag, a.end_date-now() end_date_flag, u.user_type from bidding a inner join user_info u on u.cust_id = a.cust_id where 1=1

inner join 会兼顾两张表,应该是你另一个表里面那个主键的数据有两条;;;先确定主表,用left join 或者right join 就会只有一条了 参考技术A mysql数据

ORACLE SQL语句查询一个字段在另一表字段中有两条或以上 的数据

如题:有A表,和B表,A表中的student是唯一,B表中的student不唯一,查询出来A表中哪些数据在B表中是多条而不是单条的!求解,感激不尽!

1、创建两张测试表,

create table test_student(student_id varchar2(20), student_name varchar2(20));

create table test_class(student_id varchar2(20), class_id varchar2(20));

2、插入测试数据

insert into test_student values(1001,'陈XX');

insert into test_student values(1002,'许XX');

insert into test_student values(1003,'张XX');

insert into test_student values(1004,'吴XX');

insert into test_class values (1001,'C01');

insert into test_class values (1001,'C02');

insert into test_class values (1002,'C01');

insert into test_class values (1003,'C01');

insert into test_class values (1004,'C01');

insert into test_class values (1004,'C02');

3、查询表的记录,select t.*, rowid from test_class t;

4、编写sql,查询出来test_student表中在test_class表中是多条而不是单条的记录,可以看到1001、1004学生是有多条记录的,

select t.student_id, 

       count(1) a

  from test_student t , test_class b

 where t.student_id = b.student_id

 group by t.student_id

 having count(*)>1,

参考技术A -- 先取并集,然后查询student2是否两条(根据id排序)
SELECT student FROM (SELECT student FROM A union SELECT student FROM B) a1,A a2 WHERE a2.student=a1.student and (SELECT count(*) FROM B GROUP BY student) >= 2;

-- 或者
SELECT * FROM A a, B b WHERE a.student=b.student ORDER BY b.student HAVING COUNT(*)>=2;

本回答被提问者采纳
参考技术B select b.student ,count(1) from b,a where b.student =a.student group by b.student having count(1) >1;

以上是关于mysql数据库里只有一条数据为啥查询出来有两条重复的数据的主要内容,如果未能解决你的问题,请参考以下文章

mysql如何合并两条记录,或者将查询出来的结果相连

两条mybatis sql语句第二条要用到第一条查出来的数据进行模糊查询 怎么整合成一条

mysql如何判断符合查询条件的数据有两条

sql两表联查只要左表一条数据但是查完都多一条

为啥mssql的OPENQUERY取oracle某表时只返回了一行数据,而在pl/sql中可以查到表中有两行数据,求解答?

sql 同样一条查询脚本,在2个服务器里执行耗费的时间差很多,为啥?