MYSQL 在 'WHERE' 子句中使用 'LIKE' 在子查询中搜索

MYSQL use #39;LIKE#39; in #39;WHERE#39; clause to search in subquery(MYSQL 在 WHERE 子句中使用 LIKE 在子查询中搜索)
本文介绍了MYSQL 在 'WHERE' 子句中使用 'LIKE' 在子查询中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何使用LIKE"在子查询中进行搜索?

How would you use 'LIKE' to search in a subquery?

例如我试过这样做,但不起作用:

E.g. i've tried doing this, but doesn't work:

SELECT *
FROM mytable
WHERE name
    LIKE '%
        (SELECT name FROM myothertable)
        %'

<小时>

到目前为止我有这个:


I have this so far:

SELECT * FROM t1
WHERE t1.name IN (SELECT t2.name FROM t2)
AND (t1.title IN (SELECT t2.title FROM t2)
    OR t1.surname IN (SELECT t2.surname FROM t2))

它工作正常,因为它返回完全匹配,但它似乎没有返回我的其他类似记录,所以我还想检查一下:

t1.title LIKE '%t2.title%' AND t1.surname LIKE '%t2.surname%'

我该怎么做?

It's working ok as it returns exact matchs, but it doesn't seem to return my other records that are similar, so I would like to also check that:

t1.title LIKE '%t2.title%' AND t1.surname LIKE '%t2.surname%'

How would i do this?

推荐答案

使用 JOIN:

SELECT a.*
  FROM mytable a
  JOIN myothertable b ON a.name LIKE CONCAT('%', b.name, '%')

...但是如果在 myothertable 中对于给定的 mytable 记录有多个匹配项,则可能存在重复.

...but there could be duplicates, if there's more than one match in myothertable for a given mytable record.

使用 EXISTS:

SELECT a.*
  FROM mytable a
 WHERE EXISTS (SELECT NULL 
                 FROM myothertable b 
                WHERE a.name LIKE CONCAT('%', b.name, '%'))

使用全文搜索MATCH (要求 myothertable 是 MyISAM)

Using Full Text Search MATCH (requires myothertable is MyISAM)

SELECT a.*
  FROM mytable a
  JOIN myothertable b ON MATCH(a.name) AGAINST (b.name)

这篇关于MYSQL 在 'WHERE' 子句中使用 'LIKE' 在子查询中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)