简体   繁体   English

如何编写T-SQL查询来执行“喜欢”?

[英]How can I write a T-SQL query to do a “like in”?

I need to write a valid T-SQL query version of the following pseudo-code: 我需要编写以下伪代码的有效T-SQL查询版本:

select * from newTable where [name] like in (
    select [name] from oldTable
)

I'm not sure how to go about this. 我不知道该如何解决这个问题。 Any help (even directing me to an existing question) would be great. 任何帮助(甚至指导我现有的问题)都会很棒。 Thanks! 谢谢!

Edit: Per some comments I will clarify this particular case. 编辑:根据一些评论,我将澄清这个特殊情况。 The tables look like this: 表格如下所示:

oldTable
code varchar(10)
name varchar(500)

newTable
code varchar(10)
name varchar(500)

In all of the cases where oldTable.code <> newTable.code, I am wanting to see if the oldTable.name is like one of the names in newTable.name. 在oldTable.code <> newTable.code的所有情况下,我想看看oldTable.name是否与newTable.name中的名称之一一样。 Basically, some of the new names have had qualifiers added to the beginning or end of the names. 基本上,一些新名称已将限定符添加到名称的开头或结尾。 ie: 'old name' may have a 'qualified old name' in the newTable. 即:“旧名称”可能在newTable中具有“合格的旧名称”。 Thanks again. 再次感谢。

Assuming the two tables relate in some way. 假设这两个表以某种方式相关。

SELECT newTable.* FROM newTABLE JOIN oldTable ON <JOIN CRITERIA>
WHERE newTable.[Name] LIKE oldTable.name
DECLARE @nt TABLE (NAME VARCHAR(10))
DECLARE @ot TABLE (NAME VARCHAR(10))

INSERT INTO @nt VALUES('Stuart')
INSERT INTO @nt VALUES('Ray')


INSERT INTO @ot VALUES('St%')
INSERT INTO @ot VALUES('Stu%')


SELECT *
FROM @nt n
WHERE EXISTS (SELECT *
                FROM @ot o
                WHERE n.name LIKE o.name)

Not so pretty, but it works: 不是那么漂亮,但它有效:

SELECT DISTINCT newTable.* SELECT DISTINCT newTable。*
FROM newTABLE 来自newTABLE
JOIN oldTable 加入oldTable
ON newTable."Name" LIKE oldTable.name ON newTable。“Name”LIKE oldTable.name

I think you just need to remove the like eg: 我想你只需要删除像,例如:

select * from newTable where [Name] in (select name from oldTable) select from from newTable,其中[Name] in(从oldTable中选择名称)

Thanks everyone. 感谢大家。 I used the following query, inspired by both LukLed's answer and a comment by Stuart Ainsworth. 我使用了以下查询,灵感来自LukLed的回答和Stuart Ainsworth的评论。

SELECT DISTINCT old.code, old.name, new.name, new.code 
FROM newTable new 
JOIN oldTable old
ON new.name LIKE '%' + old.name + '%' 
WHERE new.code <> old.code
ORDER BY old.name, new.name

Performance isn't that great, but it's a one time analysis and it gets the job done. 性能不是很好,但它是一次性分析,它完成了工作。

The reason I chose this over the "EXISTS" version is because it gives me both results from the new and old tables. 我在“EXISTS”版本上选择它的原因是因为它给了我新旧表的结果。

Name like Name? 姓名如姓名? well, you cannot do a LIKE and IN at the same time. 好吧,你不能同时做一个LIKE和IN。

This looks like a good candidate for SOUNDEX 这看起来像SOUNDEX的一个很好的候选人

Do a JOIN with a SOUNDEX. 与SOUNDEX一起加入。

read up here: http://msdn.microsoft.com/en-us/library/aa259235%28SQL.80%29.aspx 在这里阅读: http//msdn.microsoft.com/en-us/library/aa259235%28SQL.80%29.aspx

We ran into the same issue ourselves. 我们自己遇到了同样的问题。 It may not work for you, but the solution we came up with is: SELECT [Fields]
FROM [Table]
WHERE [Field] like 'Condition1'
OR [Field] like 'Condition2'
它可能对你不起作用,但我们提出的解决方案是: SELECT [Fields]
FROM [Table]
WHERE [Field] like 'Condition1'
OR [Field] like 'Condition2'
SELECT [Fields]
FROM [Table]
WHERE [Field] like 'Condition1'
OR [Field] like 'Condition2'

Not a great solution, but it works for us. 不是一个很好的解决方案,但它适用于我们。

If you use very rarely used cross apply you can do this with ease. 如果您使用极少使用的cross apply您可以轻松地完成此操作。
(temp table declaration stolen code from Stuart ) (来自Stuart的临时表声明被盗代码)

2 tables do not need to have any relationship as Matthews' answer . 马修斯的答案中, 2张桌子不需要有任何关系。

DECLARE @nt TABLE (NAME VARCHAR(10))
DECLARE @ot TABLE (NAME VARCHAR(10))

INSERT INTO @nt VALUES('Stuart')
INSERT INTO @nt VALUES('Ray')


INSERT INTO @ot VALUES('St%')
INSERT INTO @ot VALUES('Stu%')

select  distinct n.NAME
from    @nt n
    cross apply @ot o
where   n.NAME like o.name

A mere stab in the dark but this is awfully reminiscent of a situation involving non-scalar data. 只是在黑暗中刺伤,但这非常让人联想到涉及非标量数据的情况。 Quick example using csv format (SQL Server 2005 and above): 使用csv格式的快速示例(SQL Server 2005及更高版本):

WITH oldTable ([name])
     AS
     (
      SELECT '003,006,009,012,015'
      UNION ALL
      SELECT '005,015'
     ),
     newTable ([name])
     AS
     (
      SELECT '007'
      UNION ALL
      SELECT '009'
      UNION ALL
      SELECT '015'
     )
SELECT N1.[name]
  FROM newTable AS N1
 WHERE EXISTS (
               SELECT * 
                 FROM oldTable AS O1
                WHERE ',' + O1.[name] + ','
                         LIKE '%,' + N1.[name] + ',%' 
              );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM