简体   繁体   English

mysql显示公司的所有其他结果

[英]mysql show all other results from company

I have a question. 我有个问题。 This is my database structure 这是我的数据库结构

**company**
id | name
---------
1, Test
2, demo

**address**
id | name
---------
1, test1
2, test2
3, bla6

**address_company**
id | address_id | company_id
1, 1, 1
2, 2, 1
3, 3, 2

My query is this: 我的查询是这样的:

SELECT company.name, address.name FROM company 
INNER JOIN address_company on address_company.company_id = company.id
INNER JOIN address on address.id = address_company.address_id

This works. 这可行。 But I need to filter results. 但是我需要过滤结果。

So when people click address (frontend): test1, it only needs to show company: Test 因此,当人们单击地址(前端):test1时,只需要显示公司:Test

I can do this: 我可以做这个:

WHERE address.name = "test1"

This also works but I need to filter further so what I need is 这也可以,但是我需要进一步过滤,所以我需要的是

WHERE address.name = "test1" AND address.name = "test2" 

But this doesn't work, it doesn't show results. 但这不起作用,也不会显示结果。 I can only filter on 1 address and I need to filter on more addresses. 我只能过滤1个地址,而我需要过滤更多个地址。

Hope you guys can understand me and can help me. 希望你们能理解我并能帮助我。 THANKS! 谢谢!

Use OR instead of and, or use the in() structure: 使用OR代替and,或使用in()结构:

WHERE address.name = 'test1' OR address.name = 'test2'


WHERE address.name IN('test1', 'test2' )

Note: I hope that the below join condition was just typed incorrectly in the question: 注意:我希望以下联接条件在问题中的输入方式不正确:

 INNER JOIN address on address.id = address_company.id

The below strategy leans on the unique key(address_id,company_id) making sure there are no duplicates at that combo-level 以下策略依赖于unique key(address_id,company_id)以确保该组合级别没有重复项

Schema 架构图

create table company
(   id int auto_increment primary key,
    name varchar(100) not null
);
insert company(name) values ('Test'),('demo');

create table address
(   id int auto_increment primary key,
    name varchar(100) not null
);
insert address(name) values ('test1'),('test2'),('bla6');

create table address_company
(   id int auto_increment primary key,
    address_id int not null,
    company_id int not null,
    unique key(address_id,company_id) -- no dupes allowed ! I am banking on this below
);
insert address_company(address_id,company_id) values (1,1),(2,1),(3,2);

The queries 查询

select company_id,count(*) theCount from address_company 
where address_id in (1,2) 
group by company_id having theCount>1;

+------------+----------+
| company_id | theCount |
+------------+----------+
|          1 |        2 |
+------------+----------+

select company_id,count(*) theCount from address_company 
where address_id in (select id from address where name in ('test1','test2'))
group by company_id having theCount>1;

+------------+----------+
| company_id | theCount |
+------------+----------+
|          1 |        2 |
+------------+----------+

So if the group by / having returns greater than 1 for the count, where I literally went after name1 and name2, then I know that row qualifies. 因此,如果by by组的计数返回大于1,而我实际上是在name1和name2之后,那么我知道该行是合格的。 And that row of course then has name1 and name2. 然后,该行当然具有name1 name2。

Back to the unique key part: This assures we aren't tricked in having a company with the same address twice. 回到独特的关键部分:这确保我们不会被欺骗两次拥有相同地址的公司。 Which first off doesn't make sense, and also that would mess up this strategy. 首先是没有道理的,而且也会弄乱这种策略。

Obviously the schema needs some index help, and FK's wouldn't break anyone's heart. 显然,该架构需要一些索引帮助,而FK不会伤心任何人。 But this is just a strawman. 但这只是一个稻草人。

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

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