简体   繁体   English

SQL-根据另一列的值选择与另一列不同的值

[英]SQL - select distinct from one column based on value from another column

I am looking to run a SQL query that counts the distinct values from one column based on the value of another column being equal to X. 我希望运行一个SQL查询,该查询基于另一列的值等于X来计算一列的不同值。

So for the below, I want to count the distinct values of the "value" column where the "field_number" column is equal to 2. 因此,对于以下内容,我想计算“ field_number”列等于2的“ value”列的不同值。

The result should return "2" (Australia and New Zealand) 结果应返回“ 2”(澳大利亚和新西兰)

在此处输入图片说明

select count(distinct value)
from your_table
where field_number = 2

You can use the below query 您可以使用以下查询

select count(distinct value) from my_table where field_number = 2;

Check here SQLFIDDLE DEMO 在这里检查SQLFIDDLE DEMO

暂无
暂无

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

相关问题 如何 select 基于 SQL/php 中另一列的多个值的列中的单个值 - How to select a single value from a column based on multiple values from another column in SQL/php MYSQL查询-一种基于同一表中另一列中的DISTINCT值从一列返回所有值的简单方法? - MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table? 从两列中选择不同的 - Select distinct from two column 为表中的一列中的每个不同条目选择完整的第一行,按照另一列的顺序排列 - Select complete first row for each distinct entry in one column from a table as ordered by another 根据来自另一列的不同值对sql值求和,并将结果放入数组 - Sum sql values based on distinct values from another column and put the result in array 选择*不同的一列 - Select * distinct one column 如何从查询中选择不同的值并将其连接到一列 - How to select distinct values from query and concatenate into one column 根据一个 select 语句中另一列的不同要求对一列求和多次? - Sum a column multiple times based on different requirements from another column in one select statement? 如何从一列 select 最大值并使用选定的最大值更新另一列 - How can I select max value from one column and update another column with selected Max value 如何 SELECT DISTINCT 一列并按另一列排序? - How to SELECT DISTINCT one column and ORDER by another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM