要查询数据库中出现异常的数据,因为两种查询方式查询出来的结果有可能不一样.查询某客户最大编号的一张发票的id,第一种查询方法
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,如下
select top 1 id from purchase_customer_balance where customer_code='135' order by id desc按理说,以上两种方法查询出来的结果都是一样的,但是程序上可能有漏洞,造成了有的客户的结果不一样,怎样吧两者的查询结果不一样的数据列出来呢? 我想用exsit或者not in这样的语句来查询,但是发现以上两种方法都是指定了具体的客户名称的(customer_code='135'),查询结果都只是一条数据,我要查询所有客户,而不是指定的某个客户,现在搞头晕了,怎样写啊?
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,如下
select top 1 id from purchase_customer_balance where customer_code='135' order by id desc按理说,以上两种方法查询出来的结果都是一样的,但是程序上可能有漏洞,造成了有的客户的结果不一样,怎样吧两者的查询结果不一样的数据列出来呢? 我想用exsit或者not in这样的语句来查询,但是发现以上两种方法都是指定了具体的客户名称的(customer_code='135'),查询结果都只是一条数据,我要查询所有客户,而不是指定的某个客户,现在搞头晕了,怎样写啊?
你这是两个完全不同的查询语句,何来查询结果一样之说!
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要使它两的查询结果一样,首先,对任何一个记录,都必须满足
right(invoice_no,5) = '-' + datename(yy,getdate())
其次,你必须保证
cast(parsename(replace(invoice_no , '-' , '.'),2) as int)
与 id 的顺序完全一样,兄弟,你是如何保证的?
我们都不知道你的程序逻辑,是没法帮你排查的,有一个建议,对这两条语句,你不要 top 1 id,可以考虑 select id ,甚至用 select * 来根据两种不同的排序方式把数据列出来,看一看本该放在最后的那条记录,究竟放到哪儿去了,这才能进一步找出为什么它不在最后一条的原因.
select top 10 *
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 10 *
from purchase_customer_balance
where customer_code='135'
order by id desc我估计各用前10 条语句对比,就能找出原因来.
也就是说我们客户的发票号码如下:
jacky-37-2011
jacky-36-2011
jacky-35-2011
...从以上可以看出来,发票号最大的那个肯定是最后生成的,也就是第二种查询方法查出来得,但是现在发现最后一个发票的id竟然不是最后的情况,这当然是程序问题,是因为程序造成了数据的问题. 所以才想出来用第一种查询语句来查询,和第二种查询语句对比,来找出有问题的客户的发票