如何在具有复合键列表的表上执行 SELECT?

发布于 2024-12-11 13:42:06 字数 456 浏览 0 评论 0原文

如果我有一个包含类似列的表

ID1 ID2 VAL1 VAL2

其中ID1,ID2为主键索引。

如何针对特定 ID1、ID2 值执行 SQL 语句?

示例:我有一个包含 PK (1,1) (1,2) (2,4) (2,1) (3,1) (3,2) (3,5) 记录的表,

我只想选择包含 (1,1) (1,2) (3,5) (3,2) 的记录

SELECT 查询

从 tbl1 中选择 *,其中 ID1 IN (1,3) AND ID2 IN (1,2,5)

将产生不需要的结果:(3,1)。那么最好的方法是什么?

我正在寻找 SQL 的整体答案,但如果它依赖于 DBMS,我想知道如何在 PostgreSQL 和 MySQL 中这样做。

If I have a table with columns like

ID1 ID2 VAL1 VAL2

Where ID1,ID2 makes the primary key index.

How do I execute any SQL statements for specific ID1,ID2 values?

Example: I have table with records that has PK (1,1) (1,2) (2,4) (2,1) (3,1) (3,2) (3,5)

I want to only select records with (1,1) (1,2) (3,5) (3,2)

A SELECT query with

SELECT * FROM tbl1 WHERE ID1 IN (1,3) AND ID2 IN (1,2,5)

will yield undesired result: (3,1). So what's the best way to do this?

I am looking for an overall answer for SQL, but if it is dependent on the DBMS, I would like to know how to do so in PostgreSQL and MySQL.

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

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

发布评论

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

评论(5

掩饰不了的爱 2024-12-18 13:42:06

MySQL 和 PostgreSQL 都支持 IN 中的元组,如下所示。

SELECT * 
FROM tbl1
WHERE (ID1,ID2) IN ((1,1) (1,2) (2,4) (2,1) (3,1) (3,2) (3,5));

Both MySQL and PostgreSQL support tuples in IN as below.

SELECT * 
FROM tbl1
WHERE (ID1,ID2) IN ((1,1) (1,2) (2,4) (2,1) (3,1) (3,2) (3,5));
春花秋月 2024-12-18 13:42:06

您可以使用 andor 来做到这一点:

SELECT  * 
FROM    tbl1
WHERE   ID1 = 1 AND ID2 = 1 OR
        ID1 = 1 AND ID2 = 2 OR
        ID1 = 3 AND ID2 = 5 OR
        ID1 = 2 AND ID2 = 2

You can do that with and and or:

SELECT  * 
FROM    tbl1
WHERE   ID1 = 1 AND ID2 = 1 OR
        ID1 = 1 AND ID2 = 2 OR
        ID1 = 3 AND ID2 = 5 OR
        ID1 = 2 AND ID2 = 2
话少心凉 2024-12-18 13:42:06

这可能是执行您请求的最紧凑的方式:

SELECT *
FROM tbl1
WHERE (ID1 = 1 AND (ID2 = 1 OR ID2 = 2))
   OR (ID1 = 3 AND (ID2 = 5 OR ID2 = 2));

This is probably the most compact way of doing what you requested:

SELECT *
FROM tbl1
WHERE (ID1 = 1 AND (ID2 = 1 OR ID2 = 2))
   OR (ID1 = 3 AND (ID2 = 5 OR ID2 = 2));
画骨成沙 2024-12-18 13:42:06
SELECT * FROM tbl1 WHERE ID1 = 1 AND ID2 IN (1,2)
UNION
SELECT * FROM tbl1 WHERE ID1 = 3 AND ID2 IN (5,2) 

或者

SELECT * FROM tbl1 WHERE (ID1 = 1 AND ID2 IN (1,2)) OR (ID1 = 3 AND ID2 IN (5,2)) 
SELECT * FROM tbl1 WHERE ID1 = 1 AND ID2 IN (1,2)
UNION
SELECT * FROM tbl1 WHERE ID1 = 3 AND ID2 IN (5,2) 

OR

SELECT * FROM tbl1 WHERE (ID1 = 1 AND ID2 IN (1,2)) OR (ID1 = 3 AND ID2 IN (5,2)) 
娇柔作态 2024-12-18 13:42:06

你也可以尝试

SELECT
   *
FROM
  tbl1
  JOIN
  (
    SELECT 1 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 1 AS ID1, 2 AS ID2
    UNION ALL
    SELECT 2 AS ID1, 4 AS ID2
    UNION ALL
    SELECT 2 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 2 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 5 AS ID2
  ) as keylist ON tbl1.ID1 = keylist.ID1 AND tbl1.ID2 = keylist.ID2

You could also try

SELECT
   *
FROM
  tbl1
  JOIN
  (
    SELECT 1 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 1 AS ID1, 2 AS ID2
    UNION ALL
    SELECT 2 AS ID1, 4 AS ID2
    UNION ALL
    SELECT 2 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 1 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 2 AS ID2
    UNION ALL
    SELECT 3 AS ID1, 5 AS ID2
  ) as keylist ON tbl1.ID1 = keylist.ID1 AND tbl1.ID2 = keylist.ID2

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