SQL - 需要帮助根据行过滤结果

发布于 2024-11-08 04:21:22 字数 337 浏览 0 评论 0原文

我有一个如下所示的表:

p_id | comp_id
-----+--------
 100 | 1
 100 | 2
 101 | 1   
 102 | 1

基本上,可以根据需要多次将 p_id 输入到表中,以获得任意数量的 comp_id

我需要的是选择 comp_id 1 和 2 上的所有 p_id。在上表中,这意味着只有 p_id == 100< /code> 将被返回。

有没有有效的方法来做到这一点?

I have a table that looks like this:

p_id | comp_id
-----+--------
 100 | 1
 100 | 2
 101 | 1   
 102 | 1

Basically, the p_id can be entered multiple times into the table for as many comp_id as needed.

What I need is to select all the p_ids that have been on comp_id 1 and 2. In the above table, that would mean only p_id == 100 would be returned.

Is there an efficient way to do this?

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

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

发布评论

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

评论(6

小梨窩很甜 2024-11-15 04:21:22
SELECT p_id
FROM Mytable
WHERE comp_id IN (1,2)
GROUP BY p_id
HAVING COUNT(distinct comp_id) = 2

确保您的 IN 包含与 HAVING 子句中的 int 相同数量的值。

SELECT p_id
FROM Mytable
WHERE comp_id IN (1,2)
GROUP BY p_id
HAVING COUNT(distinct comp_id) = 2

Ensure your IN contains the same number of values as the int in the HAVING clause.

把昨日还给我 2024-11-15 04:21:22

这个怎么样:

SELECT t1.p_id
FROM table AS t1
INNER JOIN table AS t2 ON t1.p_id = t2.p_id
WHERE t1.comp_id=1
AND t2.comp_id=2

How about this:

SELECT t1.p_id
FROM table AS t1
INNER JOIN table AS t2 ON t1.p_id = t2.p_id
WHERE t1.comp_id=1
AND t2.comp_id=2
撕心裂肺的伤痛 2024-11-15 04:21:22
SELECT p_id
  FROM table 
 WHERE comp_id in (1,2)
 GROUP BY p_id
HAVING COUNT(DISTINCT comp_id) = 2

假设 #t 是下表(您正在搜索的 comp_id):

comp_id
-------
      1
      2

SELECT t1.p_id
  FROM table t1
 WHERE NOT EXITS (SELECT NULL
                    FROM table t2
              RIGHT JOIN #t
                      ON t2.p_id    = t1.p_id
                     AND t2.comp_id = #t.comp_id
                   WHERE t2.comp_id IS NULL)
SELECT p_id
  FROM table 
 WHERE comp_id in (1,2)
 GROUP BY p_id
HAVING COUNT(DISTINCT comp_id) = 2

or

assumig #t is the following table (the comp_id's you're searching for):

comp_id
-------
      1
      2

SELECT t1.p_id
  FROM table t1
 WHERE NOT EXITS (SELECT NULL
                    FROM table t2
              RIGHT JOIN #t
                      ON t2.p_id    = t1.p_id
                     AND t2.comp_id = #t.comp_id
                   WHERE t2.comp_id IS NULL)
原来分手还会想你 2024-11-15 04:21:22
SELECT p_id
FROM TABLE 
WHERE EXISTS (SELECT P_ID from TABLE where comp_id=1) and EXISTS (SELECT P_ID from TABLE where comp_id=2)
SELECT p_id
FROM TABLE 
WHERE EXISTS (SELECT P_ID from TABLE where comp_id=1) and EXISTS (SELECT P_ID from TABLE where comp_id=2)
魂牵梦绕锁你心扉 2024-11-15 04:21:22
select distinct p_id from table a 
    inner join table b on a.p_id = b.p_id where a.p_id = 1 and b.p_id = 2

这种方式可能会起作用,但似乎效率很低

select distinct p_id from table a 
    inner join table b on a.p_id = b.p_id where a.p_id = 1 and b.p_id = 2

this way would probably work but it seems pretty inefficient

苍白女子 2024-11-15 04:21:22
 SELECT p_id FROM tbl WHERE EXISTS
 (SELECT p_id FROM tbl WHERE comp_id = 1)
 AND EXISTS (SELECT p_id FROM tbl WHERE comp_id = 2)
 SELECT p_id FROM tbl WHERE EXISTS
 (SELECT p_id FROM tbl WHERE comp_id = 1)
 AND EXISTS (SELECT p_id FROM tbl WHERE comp_id = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文