简体   繁体   English

我如何 SELECT 只有表 b 中每个外键给定列的值都相同的行?

[英]How do I SELECT only rows from table b that have all the same values for a given column per foreign key?

I have a table that just has ID's.我有一张只有 ID 的表。 Another table that has ID plus a couple other columns.另一个具有 ID 和其他几个列的表。 One such column is [set], for reference.一个这样的专栏是[set],供参考。

I am trying to build a join query on only the ID's in table2 that have the same value for every row in column [set], not just ID'S that have a duplicate value in [set] plus another different value.我正在尝试仅对 table2 中的 ID 构建连接查询,这些 ID 对于列 [set] 中的每一行具有相同的值,而不仅仅是在 [set] 中具有重复值的 ID 加上另一个不同的值。 So, each ID in table2 can have multiple rows.因此,table2 中的每个 ID 都可以有多个行。

table1表格1

[id] [ID]
a1 a1
a2 a2

table2表2

[id] [ID] [op] [操作] [set] [放]
a1 a1 22 22 cut
a1 a1 21 21 cut
a2 a2 23 23 cut
a2 a2 25 25 cut
a2 a2 24 24 slice

In the given example, 'a2' would not fit the criteria because the values in column [set] are not all the same.在给定的示例中,“a2”不符合条件,因为列 [set] 中的值并不完全相同。

My query isn't working.我的查询不起作用。

SELECT DISTINCT(A.ID)
FROM TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
GROUP BY A.ID, B.SET
HAVING COUNT(DISTINCT(B.SET)) =1

You're grouping by id and set , so by definition within each group, there will be just one value for set .您按idset分组,因此根据每个组中的定义, set将只有一个值。 Remove it from the group by clause and you should be OK.group by子句中删除它,你应该没问题。 Also, not that since you're grouping by the id , you don't need a distinct on it:另外,并不是因为您按id分组,所以您不需要distinct

SELECT   A.ID -- Distinct removed. It's not wrong, just redundant
FROM     TABLE1 A
INNER    JOIN TABLE2 B ON A.ID = B.ID
GROUP BY A.ID -- Grouping by b.set removed
HAVING   COUNT(DISTINCT(B.SET)) = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择表(A)中共享相同外键(itemId)的行,其中表中的多行具有表B中的值 - How do I select rows in table (A) sharing the same foreign key (itemId) where multiple rows in table have the values in table B 我如何 SELECT 所有(表,列)引用与外键相同的列 - How do I SELECT all (table, column) referencing the same column as foreign key 如何创建一个新列来保存具有相同值的所有行的所有主键值? - How do I create a new column that holds all the primary key values of all rows that have the same value? 在SQL中,如何从表中选择*,但是当多行具有相同的字段时,只选择另一个字段B为MAX的那些? - In SQL, how do I select * from a table, but when multiple rows have the same field, select only the ones where another field B is MAX? 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? 如何仅在表中选择具有另一个表中所有值的记录? - How do I only select records in a table that have all the values from another table? 如何确保具有相同外键的所有行在我的 PostgreSQL 数据库中具有唯一名称? - How do I make sure that all rows with the same foreign key have unique names in my PostgreSQL database? 如何删除表中具有 null 外键引用的所有行? - How do I delete all rows in a table which have a null foreign key reference? 对于具有相同 A、B、C 值的行,如何在给定的时间范围内仅将 select “最早”行(由 D 列指定)? - For rows with the same A, B, C values, how to select only “earliest” row (specified by column D) within a given time range? 在 SQL 中获取列 A 中具有相同值的所有行,这些行在列 B 中只有非空值 - In SQL get all rows with same value in column A that have only non-null values in column B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM