简体   繁体   English

MySQL查询JOIN返回1个空字段

[英]MySQL Query JOIN returning 1 null field

I have two simple tables: 我有两个简单的表:

user 用户

+-------------------------------------------------+
|   uid    |     firstname     |     lastname     |
|-------------------------------------------------|
|   4000   |       Zak         |        Me        |
+-------------------------------------------------+

user_role USER_ROLE

+----------------------------------------------+
|   rid    |       uid          |     oid      |
|----------------------------------------------|
|   5      |      4000          |     7000     |
+----------------------------------------------+

I am using this query 我正在使用此查询

SELECT us.firstname, us.lastname, ur.oid
FROM user us
    LEFT JOIN user_role ur
ON us.uid = ur.uid
WHERE us.firstname = 'Zak';

My result is 我的结果是

+-------------------------------------------------+
|     firstname     |     lastname     |   oid    |
|-------------------------------------------------|
|       Zak         |        Me        |  (null)  |
+-------------------------------------------------+

What am I missing? 我想念什么? It has to be something simple! 它必须很简单!

UPDATE UPDATE

Has to do something with the WHERE clause .. Because if left out, it returns all rows with oid included WHERE子句有关系..因为如果忽略,它将返回包含oid所有行

Follow a process like this: 遵循这样的过程:

run this query 运行此查询

SELECT *
FROM user us
WHERE us.firstname = 'Zak';

If you get a result the Where clause is fine. 如果得到结果,那么Where子句就可以了。 So now you run: 所以现在您运行:

SELECT *
FROM user us
    LEFT JOIN user_role ur
ON us.uid = ur.uid
WHERE us.firstname = 'Zak';

If yuo get no records then there is something wrong with the join. 如果您没有任何记录,则说明连接有问题。 Could be that they have some unprintable characters and 4000 <>4000 as a result. 可能是他们有一些无法打印的字符,结果是4000 <> 4000。 So let's check that. 因此,让我们检查一下。

select * FROM user us where us.uid = 4000
select * FROM user_role us where us.uid = 4000

If one of then does not return a result then there is a data problem where one of the fields contains unprintable characters. 如果其中之一不返回结果,则说明存在数据问题,其中一个字段包含不可打印的字符。

If the select * works, then try the original query again only add a few other fields from the user_role table such as the uid. 如果select *有效,则再次尝试原始查询,仅从user_role表中添加一些其他字段,例如uid。 Then you can see if the join is working but the field is empty or if the join is wrong or possibly you are looking at the wrong field. 然后,您可以查看联接是否正常工作,但该字段为空,或者联接是否错误,或者您查看的字段是否错误。

SELECT us.firstname, us.lastname, ur.oid, ur.uid
FROM user us
    LEFT JOIN user_role ur
ON us.uid = ur.uid
WHERE us.firstname = 'Zak';

It is also possible the join fields are different datatypes and some type of implicit conversion is messing them up. 联接字段也可能是不同的数据类型,并且某种类型的隐式转换将它们弄乱了。 In that case you probably want to explicitly convert or preferably design your table so that they both use the same data type as they appear to be in a PK/FK relationship. 在那种情况下,您可能想要显式转换或最好设计表,以使它们都使用与看起来像是PK / FK关系的相同数据类型。

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

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