【问题标题】:SQL Select where condition : value 1 <> value 2SQL 选择 where 条件:值 1 <> 值 2
【发布时间】:2018-04-30 15:51:02
【问题描述】:

需要您的帮助以了解是否可以从具有以下条件的表中选择值:

表格内容:2个对象之间的匹配 (Id_obj_A;name_obj_A;country_obj_A;Id_obj_B;name_obj_B;country_obj_B)

Select *
from table
Where (only if country_obj_A <> country_obj_B)

非常感谢您的帮助

【问题讨论】:

  • 什么是数据类型?整数?字符串?
  • 只是一个表吗?如果是这样,那么您写的内容没有任何问题,除了 where 子句是 WHERE country_obj_A &lt;&gt; country_obj_B
  • 数据在不同的表上: Object_A = 我数据库上的实体帐户 Object_B = 另一个数据库上的实体帐户,用于验证实体信息(全局数据库) TableA: #Acct_ID,Acct_name, Acct_country TableB: #Acct_ID,Acct_name,Acct_country 表 C:#tableA.Acct_ID; #tableB.Acct_ID -> 它包含两个数据库上帐户之间的匹配所以我需要提取与帐户国家不匹配的帐户我想如果我将在两个表之间使用连接,它应该具有相同的逻辑如果我们使用一张表,条件应该是一样的^^

标签: sql if-statement select conditional-statements where


【解决方案1】:

是的。有几种方法,一种是像这样使用NOT EXISTS

select
       *
from tableA
where NOT EXISTS (
   select NULL
   from tableB
   where tableB.country_obj_B = tableA.country_obj_A
   )

或者,使用NOT IN

select
       *
from tableA
where country_obj_A NOT IN (
   select country_obj_B 
   from tableB
   )

或者,使用LEFT JOIN 然后排除连接的行:

select
       *
from tableA
left join tableB on tableA.country_obj_A = tableB.country_obj_B
where tableB.country_obj_B IS NULL

【讨论】:

  • OP 实际上是在处理两个表吗?
  • 非常感谢您的反馈 ^^ 我会在进行测试后通知您(我的 VPN 连接有问题 ^^ :所以我无法连接到服务器午餐 sql 查询)
猜你喜欢
  • 2012-05-04
  • 1970-01-01
  • 2016-03-04
  • 2018-11-16
  • 2015-10-31
  • 2023-03-07
  • 1970-01-01
  • 2013-12-25
  • 2017-01-26
相关资源
最近更新 更多