本文介绍了左联接将主键连接到普通列会造成严重破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的链接与我的问题有关

Link below is related to my question:

出色的左联接,得到意想不到的结果

这是我的表格结构,仅供参考.

Here's my table structure just to give an overview.

选项:

事件元:

嗯,这确实是同样的问题,但我认为是因为口才好.当我尝试使用PDO查询链接上的问题时.还是一样.

Well, it really is the same problem but I thought it's because of Eloquent. When I tried to use PDO querying the problem on my link. It still does the same.

$query = $db->prepare(
    "   SELECT      * FROM event_meta a
        LEFT JOIN   options b ON b.id = a.meta_value
        WHERE       a.meta_key = 'logo'
"); $query->execute();

$object = $query->fetchObject();

echo $object->value, '<br>';
echo $object->meta_value;

所以我要做的是,我有一个名为event_meta的表,并尝试连接options表以连接它们,我使用b.id,对于event_metaa.meta_value

So what I did is, I have a table called event_meta and trying to join the options table to connect them I use b.id and for event_meta is a.meta_value

我最后要发生的事情是首先检索options.value以检查其是否为空.如果为空,我将得到event_meta.meta_value的值来输出它.

What I want to happen on my end is to retrieve the options.value first to check if it's empty. If it is empty, I will get the event_meta.meta_value's value to output it.

例如,在上面的查询中,我想获取称为徽标的meta_key.它应该获得图像名称的输出.但是,由于我已连接到options表,因此应该获取options.value.输出options.value时,应为空白.由于徽标的meta_value不等于任何options.id.但这是在检索Top Up值.

So for example on my query above, I want to get the meta_key called logo. It should get the output of an image name. But since I'm connected to options table it should get the options.value. When outputting the options.value it should be blank. Since the meta_value of logo does not equals to any options.id. But it's retrieving the Top Up value.

推荐答案

您的充值" options.id值为6.event_meta.meta_value中的(大概是二进制图像)徽标值的数值为6,因为它开始"6b3b .....".这就是为什么他们比较平等.

Your "Top Up" options.id value is 6. The numeric value of your (presumably binary image) logo value in event_meta.meta_value is 6, because it starts "6b3b.....". That's why they're comparing equal.

比较数字字段和字符字段时,MySQL将获取字符字段的数字值并将其与数字进行比较.您可能缺少的是,在MySQL中,像"123abc3478uweefauiofyh3489h"这样的字符字段的数字值为123.

When comparing a numeric field with a character field, MySQL will take the numeric value of the character field and compare it with the number. The thing you may be missing is that the numeric value of a character field like "123abc3478uweefauiofyh3489h" in MySQL is 123.

将数字options.id字段设置为与JOIN中的event_meta.meta_value相同的类型.

Cast your numeric options.id field to the same type as event_meta.meta_value in your JOIN.

这篇关于左联接将主键连接到普通列会造成严重破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:54