日期:2014-05-18  浏览次数:20617 次

怎样列出两种查询方式下结果不同的情况?
要查询数据库中出现异常的数据,因为两种查询方式查询出来的结果有可能不一样.

查询某客户最大编号的一张发票的id,第一种查询方法
SQL code
select top 1 id from purchase_customer_balance where right(invoice_no,5) = '-' + datename(yy,getdate()) and customer_code='135' order by cast(parsename(replace(invoice_no , '-' , '.'),2) as int)  desc 


第二种查询方式,是查询最后生成的id,如下
SQL code
select top 1 id from purchase_customer_balance where  customer_code='135' order by id desc


按理说,以上两种方法查询出来的结果都是一样的,但是程序上可能有漏洞,造成了有的客户的结果不一样,怎样吧两者的查询结果不一样的数据列出来呢? 我想用exsit或者not in这样的语句来查询,但是发现以上两种方法都是指定了具体的客户名称的(customer_code='135'),查询结果都只是一条数据,我要查询所有客户,而不是指定的某个客户,现在搞头晕了,怎样写啊?

------解决方案--------------------
goup by + max
------解决方案--------------------
按理说?
你这是两个完全不同的查询语句,何来查询结果一样之说!
SQL code
select top 1 id 
from purchase_customer_balance 
where right(invoice_no,5) = '-' + datename(yy,getdate()) and customer_code='135' 
order by cast(parsename(replace(invoice_no , '-' , '.'),2) as int)  desc 


select top 1 id 
from purchase_customer_balance 
where  customer_code='135'
order by id desc