简体   繁体   English

为什么我的查询无法通过Mysql返回准确的结果

[英]Why my query is not returning accurate results with Mysql

I am using following Query to fetch duplicate records from a table.It is working fine for above requirements. 我正在使用以下查询从表中获取重复的记录。它可以很好地满足上述要求。

select tc_vessel_name, 
count(tc_vessel_name)as Vessel_Name from t_vessel_m as v
group by tc_vessel_name
having Vessel_Name >1

but the real problem is when i am using below query to find duplicate results having same id entry in other table then it is returning null results. 但是真正的问题是,当我使用以下查询查找在其他表中具有相同id条目的重复结果时,它返回的是空结果。 i am not able to understand the real reason behind this.Please help me on this. 我不明白这背后的真正原因。请对此提供帮助。

SELECT  tc_vessel_id,tc_vessel_name, COUNT(tc_vessel_name) AS Vesse_Name
FROM    t_vessel_m
WHERE   tc_vessel_id IN (SELECT tc_vessel_id FROM t_vessel_x) AND 
t_vessel_m.tc_is_deleted='F'
GROUP BY tc_vessel_name
HAVING ( COUNT(tc_vessel_name) > 1 )

You are not correctly finding duplicates in first query. 您没有在第一个查询中正确找到重复项。 This: 这个:

select tc_vessel_name, 
count(tc_vessel_name)as Vessel_Name from t_vessel_m as v
group by tc_vessel_name
having Vessel_Name > 1

should be this: 应该是这样的:

select tc_vessel_name, 
count(tc_vessel_name)as Vessel_Name from t_vessel_m as v
group by tc_vessel_name
having count(Vessel_Name) > 1

What you really doing is comparing not count of groups but value of grouping to 1. So first and second statements are different. 您实际上所做的不是比较组数,而是将组的值与1进行比较。因此,第一条语句和第二条语句是不同的。 Look at this fiddle http://sqlfiddle.com/#!9/f5895/1 There are no dups but you get 2 rows as a result. 看看这个小提琴http://sqlfiddle.com/#!9/f5895/1没有小样,但是结果是2行。 In Sql Server it would give you a cast exception, but it implicitly converts 1 to varchar in MySql . Sql Server ,它将为您提供强制转换异常,但在MySql中将1隐式转换为varchar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM