简体   繁体   English

内联接MYSQL查询不起作用

[英]Inner join MYSQL query not working

I have a table structure with three tables: profiles, profile_subrubriek and rubrieken. 我有一个包含三个表的表结构:profiles,profile_subrubriek和rubrieken。 I query the data with the following query: 我使用以下查询查询数据:

SELECT profiles.hoofdrubriek, profiles.plaats
, profiles.bedrijfsnaam, profiles.gemeente, profiles.bedrijfsslogan
, profiles.straatnaam, profiles.huisnummer, profiles.postcode
, profiles.telefoonnummer, profiles.fax, profiles.email
, profiles.website, profiles.bedrijfslogo 

FROM profiles INNER JOIN profile_subrubriek ON profiles.ID=profile_subrubriek.profile_id 
INNER JOIN rubrieken ON profile_subrubriek.subrubriek_id=rubrieken.ID  

where (
rubrieken.rubriek = 'Aannemersbedrijven' 
OR 
profiles.hoofdrubriek = 'Aannemersbedrijven') 
AND profiles.gemeente = 'Dongen'

The result, 0 rows. 结果,0行。 That is not correct. 这是不正确的。 If I take out the Inner Join and only incorporate the 'hoofdrubriek' column in the WHERE clausule I get about 25 rows as a result, that is correct. 如果我取出Inner Join并且只在WHERE clausule中包含'hoofdrubriek'列,那么我得到大约25行,这是正确的。 So this query (modified version of the above) does actually work: 所以这个查询(上面的修改版本)确实有效:

SELECT profiles.hoofdrubriek, profiles.plaats, profiles.bedrijfsnaam
, profiles.gemeente, profiles.bedrijfsslogan, profiles.straatnaam
, profiles.huisnummer, profiles.postcode, profiles.telefoonnummer
, profiles.fax, profiles.email, profiles.website, profiles.bedrijfslogo 

FROM profiles where (profiles.hoofdrubriek = 'Aannemersbedrijven') 
AND profiles.gemeente = 'Dongen'

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

Probably the joined tables don't contain referenced values. 可能联接的表不包含引用的值。 Try LEFT JOIN instead of INNER JOIN . 尝试LEFT JOIN而不是INNER JOIN

Start troubleshooting with this query. 使用此查询开始排除故障。

select count(*) records
FROM profiles INNER JOIN profile_subrubriek ON profiles.ID=profile_subrubriek.profile_id 

If records is greater than 1, add this line and run it again: 如果记录大于1,请添加此行并再次运行:

INNER JOIN rubrieken ON profile_subrubriek.subrubriek_id=rubrieken.ID  

Keep adding bits of your original query, one by one, until records is zero. 继续逐个添加原始查询的位,直到记录为零。 The last thing you added will be the reason. 你添加的最后一件事就是原因。

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

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