简体   繁体   English

如何只选择一列唯一而另一列是特定值的行

[英]How to only select rows where one column is unique and the other is a certain value

I am trying to figure out which ids only have one type of transaction.我想弄清楚哪些 ID 只有一种类型的交易。

I have tried joining and selecting distinct but I am doing it wrong我试过加入并选择不同的但我做错了

select transactions.type, id 
from datasource
group by id, transactions.type

This gives me a table with two transaction types: either dep or withdraw.这给了我一个包含两种事务类型的表:dep 或withdraw。 Most IDs have two rows, one being dep and the other withdraw.大多数ID有两行,一是dep,另一行是withdraw。 I want to select only the ids that have only the withdraw transaction type我只想选择只有提款交易类型的 ID

use where condition and not exists使用 where 条件和not exists

select transactions.type, id from datasource t1
where type='withdraw'
and not exists( select 1 from datasource t2 where t1.id=t2.id
               and type='dep')

You can use group by with having count(*)=1 clause您可以将group byhaving count(*)=1子句一起使用

select id 
  from datasource 
 where transactions_type in     
    (
     select transactions_type, id 
       from datasource
      group by transactions_type
     having count(*)=1 )

Since "You are trying to figure out which ids only have one type of transaction"由于“您正在尝试找出哪些 ID 只有一种类型的交易”

If you specifically look for transactions_type = 'withdraw' , then add and transactions_type = 'withdraw' to the end of the above Select Statement.如果您专门查找transactions_type = 'withdraw' ,则将and transactions_type = 'withdraw'到上述 Select 语句的末尾。

PS I suppose there's a type for transactions.type , and that should be transactions_type , isn't that? PS 我想transactions.type有一个类型,应该是transactions_type ,不是吗?

I think aggregation does what you want.我认为聚合可以满足您的需求。 However, I am confused what your data structure is.但是,我很困惑你的数据结构是什么。 A join is needed somewhere:某处需要join

select ds.id, min(t.type)
from datasource ds join
     transactions t
     on ds.? = t.?   -- what is the join key?
group by ds.id
having min(t.type) = max(t.type);

If transactions.type is actually a column in datasource , then:如果transactions.type实际上是datasource一列,则:

select ds.id, min("transactions.type")
from datasource ds 
group by ds.id
having min("transactions.type") = max("transactions.type");

Similar to other answers, but simpler:与其他答案类似,但更简单:

select id, count(distinct transactions.type)
from datasource
group by id
having count(distinct transactions.type) = 1

It is unclear, however, what transactions.type is.然而,不清楚transactions.type 是什么。 It doesn't appear to be valid as a column name.它似乎作为列名无效。 Or did you mean to write transactions_type ?或者你的意思是写transactions_type

暂无
暂无

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

相关问题 MySQL:如何在给定其他某个列值的情况下选择其中两个列值的组合唯一的行 - Mysql: how to select rows where the combination of 2 of the column values are unique given a certain other column value SQL选择行,其中列值是唯一的(仅出现一次) - SQL select rows, where column value is unique (only appears once) 如何 select 仅记录在某个列中包含唯一值 - How to select only records that contain a unique value in a certain column 如何不 SELECT 某些列相同而一列不同的行? - How to not SELECT rows where certain columns are the same and one column is different? 表的SELECT计数,其中列为CONNECT,但与其他相关列相关的其他行没有特定值 - SELECT count from table where column is CONNECT but other rows with related other column does not have a certain value 如何 select 只为列的每个唯一值的第一行? - How to select only the first rows for each unique value of a column? 如何使用sql为列的每个唯一值仅选择第一行 - How to select only the first rows for each unique value of a column with sql 仅选择某些列仅包含特定值的ID - Select only IDs where a certain column only contains a certain value POSTGRES:仅当另一个值不存在时,如何 select 行具有某个值,在这种情况下 select 另一个值? - POSTGRES: How to select rows with a certain value only if another value doesn't exist, and in that case select the other value? Select 行,其中列值相对于列是唯一的 - Select rows where column value is unique in relation to a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM