选择一个值,然后在同一个表中选择该值,并在SQL中进行比较
我需要找到价值不等于相关客户价值的客户NUM。 请帮帮我。只要:选择一个值,然后在同一个表中选择该值,并在SQL中进行比较,sql,Sql,我需要找到价值不等于相关客户价值的客户NUM。 请帮帮我。只要: Customer num. Value. Related Customer num 10001. 5000. 10002 20001 3500. 20002 10002. 4000. 10001 20002. 3500.
Customer num. Value. Related Customer num
10001. 5000. 10002
20001 3500. 20002
10002. 4000. 10001
20002. 3500. 20001
这可以确保,如果相关客户存在记录,则其值不相同
如果您还想检查相关客户的记录是否存在:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where t1.customer_num = t.related_customer_num and t1.value <> t.value
)
选择
t1.customerNum
从…起
表t1
左连接
t1.relatedCustomerNum=t2.customerNum上的表t2
哪里
t1.值t2.值
获取所有记录并与相关记录匹配。如果需要,还可以添加几个过滤器。例如,t1不为null,这将删除没有相关记录的记录欢迎访问该站点。我建议包括您的createtableddl,以便人们能够更好地理解表结构。此外,答案可能具有特定于品牌的语法,因此您可能应该使用您正在使用的SQL的特定于品牌的标记来标记此问题,例如SQL Server或MySQL等。。这将有助于将您的问题定位于合适的专家。
select t.*
from mytable t
inner join mytable t1
on t1.customer_num = t1.related_customer_num
and t1.value <> t.value