MYSQL子集操作

发布于 2024-11-09 11:13:34 字数 350 浏览 0 评论 0原文

有没有办法实现类似的目标:

SELECT *
FROM tb_values
WHERE (value1, value2, value3) SUBSET_OF
    ( SELECT value
      FROM tb_value
      WHERE isgoodvalue = true
    )

更多信息: 我有一个名为项目的表。每个项目都有标签。一个标签可以被多个项目共享。有一个名为projectTagMap 的映射表。现在用户使用标签来过滤项目。使用 UI 上的复选框选择标签。因此,用户选择多个标签来过滤项目。我应该从包含用户选择的所有标签的项目表中选择项目。

Is there a way to achieve something like:

SELECT *
FROM tb_values
WHERE (value1, value2, value3) SUBSET_OF
    ( SELECT value
      FROM tb_value
      WHERE isgoodvalue = true
    )

More information:
I have a table called projects. Each project has tags. A tag can be shared by multiple projects. There is a mapping table called projectTagMap. Now users use tags to filter the projects. Tags are selected using checkboxes on the UI. So a user selects several tags to filter the projects. Am supposed to select the projects from projects table that contains all the tags selected by the user.

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

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

发布评论

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

评论(2

甜味拾荒者 2024-11-16 11:13:34

从您的伪代码中,我猜您想检查(动态)值列表是否是 SELECT 提供的另一个列表的子集。如果是,那么将显示整个表格。如果没有,则不会显示任何行。

以下是实现这一目标的方法:

SELECT *
FROM tb_values
WHERE 
    ( SELECT COUNT(DISTINCT value)
      FROM tb_value
      WHERE isgoodvalue = true
        AND value IN (value1, value2, value3)
    ) = 3

在OP的解释之后更新

SELECT *
FROM project
  JOIN 
    ( SELECT projectid
      FROM projectTagMap
      WHERE isgoodvalue = true
        AND tag IN (tag1, tag2, tag3)
      GROUP BY projectid
      HAVING COUNT(*) = 3
    ) AS ok
    ON ok.projectid = project.id

From your pseudo code I guess that you want to check if a (dynamic) list of values is a subset of another list provided by a SELECT. If yes, then a whole table will be shown. If not, no rows will be shown.

Here's how to achieve that:

SELECT *
FROM tb_values
WHERE 
    ( SELECT COUNT(DISTINCT value)
      FROM tb_value
      WHERE isgoodvalue = true
        AND value IN (value1, value2, value3)
    ) = 3

UPDATED after OP's explanation:

SELECT *
FROM project
  JOIN 
    ( SELECT projectid
      FROM projectTagMap
      WHERE isgoodvalue = true
        AND tag IN (tag1, tag2, tag3)
      GROUP BY projectid
      HAVING COUNT(*) = 3
    ) AS ok
    ON ok.projectid = project.id
抚你发端 2024-11-16 11:13:34

可能是一种原始方法,但我想你可以这样做:

WHERE value1 IN (SELECT value FROM tb_value WHERE isgoodvalue = true) OR value2 IN (...) ...

Probably a primitive method but i suppose you could do:

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