[英]How to select rows with same values in one column but all must be different between each other in another column
Hello everyone i will try to be as specific and precise as possible. 大家好,我会尽力做到尽可能具体和精确。
I was looking around for answer to my problem and in mean time i made 2 tables 我一直在寻找问题的答案,同时我做了2张桌子
In first table i have names of Companies and their id-s. 在第一个表格中,我有公司名称及其ID。
In second table i have names of Employees,their id-s,the cities in which they live,and idK-s(its supposed to match id id-s from previous table) of Companies they work in. 在第二张表中,我有雇员的姓名,他们的身份证号,他们居住的城市以及他们工作的公司的idK-s(应该与上一张表的id-s相匹配)。
I am supposed to select names of Companies that have at least four(3 or more) employees living in different cities compared to each other. 我应该选择至少有四名(3名或更多)雇员住在不同城市的公司名称。
In order for things to be more clear i am going to give an example: 为了使事情更清楚,我将举一个例子:
table1: 表格1:
id name
1 Company1
2 Company2
3 Company3
4 Company4
5 Company5
6 Company6
table2: 表2:
id name city idK
1 EMP1 city1 1
2 EMP2 city2 1
3 EMP3 city3 1
4 EMP4 city4 1
5 EMP5 city1 2
6 EMP6 city2 2
7 EMP7 city3 2
8 EMP8 city1 3
9 EMP9 city2 3
10 EMP10 city3 3
11 EMP11 city1 4
12 EMP12 city2 4
13 EMP13 city3 4
14 EMP14 city4 4
15 EMP15 city5 4
16 EMP16 city1 5
17 EMP17 city2 5
18 EMP18 city1 5
19 EMP19 city3 5
20 EMP20 city3 5
21 EMP21 city2 5
22 EMP22 city1 6
23 EMP23 city8 6
24 EMP24 city1 6
25 EMP25 city15 6
I made spaces so it is easier to see. 我做了空格,所以更容易看到。 So in idK=1 we have four employees and all are from different city compared to each other so Company1 should be in results. 因此,在idK = 1中,我们有四名员工,并且所有人都来自不同的城市,因此Company1应该在结果中。
idK=2 and idK=3 have under four employees in general so Company2 and Company3 are out. idK = 2和idK = 3的雇员总数通常不足四个,因此Company2和Company3缺席。
idK=4 is good same reasons as idK=1 but idK=5 and idK=6 are out because while they have four or more employees they are not from different cities compared to each other. idK = 4与idK = 1的理由相同,但是idK = 5和idK = 6不在,因为他们有四个或更多的员工,但彼此相比来自不同城市。
I found similar problems as mine and some solutions but none were good enough for me or i just couldn't make them work so i would be very grateful if someone could explain to me what to do in this situation. 我发现了与我类似的问题和一些解决方案,但没有一个对我足够好,或者我无法使它们正常工作,因此,如果有人可以向我解释在这种情况下该怎么办,我将不胜感激。
You can use count(DISTINCT columnname) to get the number of unique values in a column. 您可以使用count(DISTINCT columnname)来获取列中唯一值的数量。
SELECT
c.`name`,
COUNT(DISTINCT e.`city`) as `Num Cities`
FROM `companies` c
JOIN `employees` e
ON c.`id` = e.`idK`
WHERE COUNT(DISTINCT e.`city`) > 3
GROUP BY c.`name`
ORDER BY c.`name`
I expect this is what you are looking for 我希望这是您要寻找的
select idk
from table2
group by idk
having count(distinct city) > 3
This will work here. 这将在这里工作。
select t1.name from table2 t2
inner join table1 t1 on t2.idK = t1.id
group by t2.idk,t1.name
having count(distinct city) > 3
This is the data I used to test on db-fiddle. 这是我用来在db-fiddle上测试的数据。
CREATE TABLE table2 (id int, name varchar(100), city varchar(100), idK int);
INSERT INTO table2 (id,name,city,idK)
VALUES (1,'EMP1','city1',1),
(2,'EMP2','city2',1),
(3,'EMP3','city3',1),
(4,'EMP4','city4',1),
(5,'EMP5','city1',2),
(6,'EMP6','city2',2),
(7,'EMP7','city3',2),
(8,'EMP8','city1',3),
(9,'EMP9','city2',3),
(10,'EMP10','city3',3),
(11,'EMP11','city1',4),
(12,'EMP12','city2',4),
(13,'EMP13','city3',4),
(14,'EMP14','city4',4),
(15,'EMP15','city5',4),
(16,'EMP16','city1',5),
(17,'EMP17','city2',5),
(18,'EMP18','city1',5),
(19,'EMP19','city3',5),
(20,'EMP20','city3',5),
(21,'EMP21','city2',5),
(22,'EMP22','city1',6),
(23,'EMP23','city8',6),
(24,'EMP24','city1',6),
(25,'EMP25','city15',6);
Create TABLE table1 (id int,name varchar(100));
INSERT INTO table1 (id,name)
VALUES (1,'Company1'),
(2,'Company2'),
(3,'Company3'),
(4,'Company4'),
(5,'Company5'),
(6,'Company6');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.