简体   繁体   English

SQL查询显示左联接没有结果

[英]SQL Query shows no result with left join

I have the following query: 我有以下查询:

SELECT u.user_id, u.username, u.email
                   hp.homepage_id
              FROM table_u u
              LEFT JOIN table_hp hp
                ON (u.user_id = hp.user_id)
             WHERE u.blocked = 'N'
               AND u.email LIKE 'someemailaddress'

I am joining on the user_id column, for the given emailadress I know both properties are the same so I should get a result but still I don't get any result... so what is wrong with this query? 我正在加入user_id列,对于给定的emailadress,我知道两个属性都相同,因此我应该得到一个结果,但仍然没有任何结果...所以此查询出了什么问题?

Put the '%' before and after in the LIKE clause - see below 在LIKE子句的前后放置'%'-参见下文

SELECT u.user_id, u.username, u.email
                   hp.homepage_id
              FROM table_u u
              LEFT JOIN table_hp hp
                ON (u.user_id = hp.user_id)
             WHERE u.blocked = 'N'
               AND u.email LIKE '%someemailaddress%'

Did you try the query without LEFT JOIN ?? 您是否尝试了没有LEFT JOIN的查询?

SELECT u.user_id, u.username, u.email 
FROM table_u u 
WHERE u.blocked ='N' 
AND u.email LIKE 'someemailaddress'

Does it return any result? 它返回任何结果吗? Cause LEFT JOIN just append data for the existing data from your "base table" .. so if the LEFT JOIN didn't find any user_id in table_hp there should be some data returned, if there are some from the query above 因为LEFT JOIN只是将数据添加到“基本表”中的现有数据中..因此,如果LEFT JOIN在table_hp中未找到任何user_id,则应该返回一些数据,如果上面的查询中有一些数据

The point was how some data was stored in the database... Seems that in an old (buggy) version of the application data was stored without decent trimming etc. The issue was the field was stored in with a whitespace... 关键是如何在数据库中存储一些数据...似乎在应用程序的旧(原始)版本中存储的数据没有适当的修整等。问题是该字段使用空格存储...

Stupid... should/must had checked the actual data that was stored... 愚蠢...应该/必须检查已存储的实际数据...

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

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