简体   繁体   English

如何计算具有特定值的行(ID),如果MySQL中相同的ID具有其他值,该如何排除呢?

[英]How to count rows (IDs) that have a specific value and exclude if the same ID has other value in MySQL?

I need to count Companies that have Field_ID "134" but don't have other Field_IDs; 我需要计算Field_ID为“ 134”但没有其他Field_ID的公司; in my example Company with ID 2 has only 1 field "134" so I need this to be counted, but company with ID 3 has field "134" but other fields as well, so I want to exclude this one from counting. 在我的示例中,ID为2的公司只有1个字段“ 134”,因此我需要对此进行计数,但是ID 3的公司却具有“ 134”字段,但还有其他字段,因此我想从计数中排除该字段。

Table ID        Company_ID  Field_ID
1               1              130
2               1              131
3               2              134
4               3              134
5               3              137
6               3              140

MySQL Workbench MySQL工作台

SELECT count(distinct id) FROM company
where field_id = 1 

I need to count Companies that have Field_ID "134" but don't have other Field_IDs; 我需要计算Field_ID为“ 134”但没有其他Field_ID的公司; in my example Company with ID 2 has only 1 field "134" so I need this to be counted, but company with ID 3 has field "134" but other fields as well, so I want to exclude this one from counting. 在我的示例中,ID为2的公司只有1个字段“ 134”,因此我需要对此进行计数,但是ID 3的公司却具有“ 134”字段,但还有其他字段,因此我想从计数中排除该字段。

Expected result should be count of "1" 预期结果应为“ 1”

Table ID        Company_ID  Field_ID
1               1              130
2               1              131
3               2              134
4               3              134
5               3              137
6               3              140
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(Company_ID  INT NOT NULL,Field_ID INT NOT NULL,PRIMARY KEY(company_id,field_id));

INSERT INTO my_table VALUES
(1,130),
(1,131),
(2,134),
(3,134),
(3,137),
(3,140);

SELECT DISTINCT x.* 
           FROM my_table x 
           LEFT 
           JOIN my_table y 
             ON y.company_id = x.company_id 
            AND y.field_id <> x.field_id 
          WHERE x.field_id = 134 
            AND y.company_id IS NULL;
+------------+----------+
| Company_ID | Field_ID |
+------------+----------+
|          2 |      134 |
+------------+----------+

You can also use HAVING MAX(field_id) = 134 AND MIN(field_id) = 134, or variations on that theme 您还可以使用HAVING MAX(field_id)= 134 AND MIN(field_id)= 134,或该主题的变体

You can group by Company_ID and count the number of Field_ID , then select only the ones that have that count = 1 : 您可以按Company_ID分组并计算Field_ID的数量,然后仅选择计数= 1的那些:

Schema (MySQL v5.7) 模式(MySQL v5.7)

CREATE TABLE test (
  `Table ID` INTEGER,
  `Company_ID` INTEGER,
  `Field_ID` INTEGER
);

INSERT INTO test
  (`Table ID`, `Company_ID`, `Field_ID`)
VALUES
  (1, 1, 130),
  (2, 1, 131),
  (3, 2, 134),
  (4, 3, 134),
  (5, 3, 137),
  (6, 3, 140);

Query #1 查询#1

SELECT t.Company_ID
FROM test t
GROUP BY t.Company_ID
HAVING COUNT(t.Field_ID) = 1
AND MAX(t.Field_ID) = 134;

Output 输出量

| Company_ID |
| ---------- |
| 2          |

View on DB Fiddle 在数据库小提琴上查看

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM