MySQL 查询通过多个 WHERE 提取不同的 ID

发布于 2024-10-15 05:49:32 字数 710 浏览 7 评论 0原文

我的大脑被冻结了,我原本以为这是一个简单的查询。

我有一个如下表:

ID   DETAILID    NAME    
-------------------------------------------------
1    1           Sed ut perspiciatis unde
2    1           omnis iste natus error
3    1           sit voluptatem accusantium
4    1           doloremque laudantium
5    2           totam rem aperiam
6    2           labore et dolore
7    3           voluptate velit esse
8    3           occaecati cupiditate non
9    3           culpa qui officia
10   3           placeat facere possimus
11   3           Nam libero tempore

我想使用 LIKE '%%' 多次搜索 NAME 字段(LIKE 的数量是随机的),但我只想返回一个不同的 DETAILID,其中 LIKE 全部在记录中找到,其中DETAILID 是相同的。

我在兜圈子,却一无所获。有人可以帮忙吗?

I am having a brain freeze with what originally I thought would be a simple query.

I have a table like the following:

ID   DETAILID    NAME    
-------------------------------------------------
1    1           Sed ut perspiciatis unde
2    1           omnis iste natus error
3    1           sit voluptatem accusantium
4    1           doloremque laudantium
5    2           totam rem aperiam
6    2           labore et dolore
7    3           voluptate velit esse
8    3           occaecati cupiditate non
9    3           culpa qui officia
10   3           placeat facere possimus
11   3           Nam libero tempore

I want to search the NAME field multiple times with LIKE '%%' (number of LIKE's will be random) but I only want to return a distinct DETAILID where the LIKE's are all found in records where the DETAILID is the same.

I am going round in circles and getting nowhere fast. Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冧九 2024-10-22 05:49:32

此查询为您提供与所有 LIKE 条件匹配的 DETAILID

SELECT DETAILID
FROM
(
    select DETAILID, 1 as WhichMatch
    from tbl
    where NAME LIKE '%a%'
    UNION ALL
    select DETAILID, 2 as WhichMatch
    from tbl
    where NAME LIKE '%b%'
    UNION ALL
    select DETAILID, 3 as WhichMatch
    from tbl
    where NAME LIKE '%c%'
) SQ
GROUP BY DETAILID
HAVING COUNT(DISTINCT WhichMatch) = 3

This query gives you the DETAILIDs that match all the LIKE conditions

SELECT DETAILID
FROM
(
    select DETAILID, 1 as WhichMatch
    from tbl
    where NAME LIKE '%a%'
    UNION ALL
    select DETAILID, 2 as WhichMatch
    from tbl
    where NAME LIKE '%b%'
    UNION ALL
    select DETAILID, 3 as WhichMatch
    from tbl
    where NAME LIKE '%c%'
) SQ
GROUP BY DETAILID
HAVING COUNT(DISTINCT WhichMatch) = 3
三生池水覆流年 2024-10-22 05:49:32

因为您正在寻找要找到的所有“LIKE”值,所以您需要在它们之间使用 OR,需要 COUNT(*) 来查看有多少匹配项,每个 ID 需要一个 GROUP BY,以及一个 HAVING 来匹配您期望的计数...

select 
      DETAILID,
      count(*) RecsFound
   from 
      YourTable
   where 
          NAME LIKE '%a%'
       or NAME LIKE '%b%'
       or NAME LIKE '%c%'
       or NAME LIKE '%d%'
   group by
      DetailID
   having
      RecsFound = 4

正如您所提到的,它可能是随机数量的“like”限定符,听起来像是一个动态构建的 SQL 查询。因此,请确保您的 HAVING 子句计数与您正在测试的条目相匹配。

编辑 - 修改后的答案将 IF() 移动到

select 
      DETAILID
   from 
      YourTable
   where 
          if( NAME LIKE '%a%', 1, 0 ) 
        + if( NAME LIKE '%b%', 1, 0 ) 
        + if( NAME LIKE '%c%', 1, 0 ) 
        + if( NAME LIKE '%d%', 1, 0 ) = 4

我想我是从多行表中查看它的部分,其中每个条目都是您正在测试的自己的元素。您希望在同一行中匹配所有喜欢的所有值...虽然您已经接受了另一个答案,但这里有另一个替代方案,不使用所有不同的联合...

此外,遍历表一次而不是 2、3,通过所有工会 4 个或更多,然后进行分组... 它达到一次记录,符合所有 4 个条件并完成。

Because you are looking for ALL "LIKE" values to be found, you'll need an OR between them, a COUNT(*) to see how many match, and a GROUP BY per ID, and a HAVING to match the count you expect...

select 
      DETAILID,
      count(*) RecsFound
   from 
      YourTable
   where 
          NAME LIKE '%a%'
       or NAME LIKE '%b%'
       or NAME LIKE '%c%'
       or NAME LIKE '%d%'
   group by
      DetailID
   having
      RecsFound = 4

As you mentioned, it could be a random number of "like" qualifiers and sounds like it will be a dynamically constructed SQL Query. As such, make sure your HAVING clause count matches those entries you are testing against.

EDIT - revised answer move IF() to where section

select 
      DETAILID
   from 
      YourTable
   where 
          if( NAME LIKE '%a%', 1, 0 ) 
        + if( NAME LIKE '%b%', 1, 0 ) 
        + if( NAME LIKE '%c%', 1, 0 ) 
        + if( NAME LIKE '%d%', 1, 0 ) = 4

I guess I was looking at it from a multi-rowed table where each entry was its own element you were testing for. You are looking to have all values in the same ROW matching ALL the LIKEs... Although you have accepted another answer, here's another alternative WITHOUT using all the distinct unions...

Additionally, goes through the table ONCE instead of 2, 3, 4 or more via all the unions, then having a group by... It hits the record ONCE, qualifies all 4 conditions and is DONE.

千里故人稀 2024-10-22 05:49:32

这不行吗?

SELECT DISTINCT DETAILID 
FROM MyTable
WHERE NAME LIKE '%something%'
   or NAME LIKE '%sometingelse%

Wouldn't this work?

SELECT DISTINCT DETAILID 
FROM MyTable
WHERE NAME LIKE '%something%'
   or NAME LIKE '%sometingelse%
征棹 2024-10-22 05:49:32

好吧,在阅读了您对其他两个答案的评论后,我相信您需要这个 -

在查询中使用 AND 而不是 OR -

SELECT DISTINCT DETAILID 
FROM MyTable
WHERE NAME LIKE '%something%'
   AND NAME LIKE '%someting1%'
   AND NAME LIKE '%someting2%'

Well, after reading your comments on other two answers, I believe you need this -

Use AND instead of OR in the query -

SELECT DISTINCT DETAILID 
FROM MyTable
WHERE NAME LIKE '%something%'
   AND NAME LIKE '%someting1%'
   AND NAME LIKE '%someting2%'
丑丑阿 2024-10-22 05:49:32

我认为最简单的解决方案是在多个查询中执行此操作。
使用 WHERE 选择非重复 id 的 count() 并选择不使用 where 条件的非重复 id 的 count()。
比较这两个将是确定“detialids”的所有“名称”是否与您的“LIKE”匹配的完美方法

I think the easiest solution is to do it in multiple queries.
Selecting the count() of the distinct id with a WHERE and selecting the count() of the distinct id without where condition.
Comparing those 2 would be a perfect way of determining wether all 'names' of 'detialids' match your LIKE's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文