【问题标题】:Match column1 from table1 with column1 in table2 and return * records from table2 which are matched with column1 of table1将table1中的column1与table2中的column1匹配,并返回table2中与table1的column1匹配的*记录
【发布时间】:2021-04-17 06:09:54
【问题描述】:

我试过这个:

select *
from table2
where exists (
    select *
    from table1
    where table2.column1 = table1.column1
)

输出:它返回 table2 中列的所有表头,但不返回任何记录。

观察:当我尝试匹配值间列时,它正在返回记录。但它在匹配字符值列时不起作用

注意:table1 和 table2 中的 column1 都有字符值。

select [Data Name]
from [DynamicDataTable]
where exists (
    select *
    from University_Temp
    where DynamicDataTable.autocode = University_Temp.autocode
)

上述查询的输出

[Data Name]
Title  
First Name  
Geebee Centre  
Country of Permanent Residence  
SSC Institute/College  

上述查询返回匹配的自动代码记录,数据类型为整数。

【问题讨论】:

  • 你能试试运行这段代码select * from table2, table1 where table2.column1=table1.column1;吗?它输出任何记录吗?
  • 您能否也发布您提到的代码在您的问题中有效(匹配值间列)?
  • 请编辑您的问题并将任何相关信息放在问题中,而不是在 cmets 中。您可以编辑您的问题here
  • 发布有效的查询无法帮助我们帮助您解决无效的查询。发布示例数据并发布您想要的结果。
  • 我们需要查看查询失败的数据。请发布示例数据

标签: sql sql-server database


【解决方案1】:

所以您想查看table2 中的所有行,其中Column1table1 中的Column1 匹配?
如果我没听错的话,这只是一个简单的加入

select t2.*
from   table2 t2
  inner join table1 t1 on t2.Column1 = t1.Column1

如果这不是您想要的,请编辑您的问题并更详细地解释您需要什么

【讨论】:

  • 也许他想得到第二张表的匹配记录,所以可以使用左连接
  • 与此查询的结果相同,问题是它与整数数据类型列一起工作正常,而不是与 nvarchar 数据类型列一起工作,如果我匹配名称之类的列它不起作用,例如,t2.col1 (数据类型为nvarchar)=t1.col1(nvarchar数据类型)
  • 两个表中的两列是否具有相同的数据类型?请向我们展示一些此查询不起作用的示例数据
  • @MohammadFahadRao 可能是的,这就是为什么我在回答中更详细地解释他想要什么
  • 我们需要查看查询失败的数据。请发布示例数据
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 2022-01-28
  • 2016-01-20
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多