Inner join MYSQL query not working

元气小坏坏 提交于 2019-12-11 22:22:52

问题


I have a table structure with three tables: profiles, profile_subrubriek and 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. 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. 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!


回答1:


Probably the joined tables don't contain referenced values. Try LEFT JOIN instead of INNER JOIN.




回答2:


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:

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.



来源:https://stackoverflow.com/questions/19349626/inner-join-mysql-query-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!