Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 当PK行(1对无/多)的可选FK行不存在时,更正sql查询以返回null_Mysql_Sql_Outer Join_Table Alias - Fatal编程技术网

Mysql 当PK行(1对无/多)的可选FK行不存在时,更正sql查询以返回null

Mysql 当PK行(1对无/多)的可选FK行不存在时,更正sql查询以返回null,mysql,sql,outer-join,table-alias,Mysql,Sql,Outer Join,Table Alias,为mysql编写此查询的正确方法是什么?此方法似乎可行,但似乎很愚蠢。191是一个变量的硬代码 select t1.item_id, t1.item_name, t1.item_desc, t.quantity, t.price from (select * from items i where i.item_id = 191) as T1 LEFT JOIN (select * from item_properties ip) as T on t1.item_id = t.fk_item_i

为mysql编写此查询的正确方法是什么?此方法似乎可行,但似乎很愚蠢。191是一个变量的硬代码

select t1.item_id, t1.item_name, t1.item_desc, t.quantity, t.price 
from (select * from items i where i.item_id = 191) as T1
LEFT JOIN (select * from item_properties ip) as T
on t1.item_id = t.fk_item_id and t1.item_id=191;
T1.item_id为主键,T.fk_item_id为外键?-仅当父项T1.item_id存在时才能存在

这是我在t.values不存在且无法联接时将其返回为null的方法


如果您想了解如何在mysql中正确实现连接,请感谢

若您只想在两个表中的匹配项都使用FK和PK的内部联接时得到结果

SELECT
    i.item_id, i.item_name, i.idem_desc,
    p.quantity, p.price
FROM items i
LEFT JOIN item_properties p ON i.item_id = p.fk_item_id
WHERE i.item_id = 191

左联接是必需的,所以即使右表中没有匹配的行,也总是返回左表中的行。但是那些从中选择的是不必要的。尽量让事情简单化。

我感觉到的问题是,我不确定FK row是否存在。。。因此,尝试使用不存在的FK进行内部联接时,会排除PK结果。您可以编辑您的问题吗?显示您的表格结构和预期结果,如下图所示,单击链接编辑您的问题,以便我们能够了解您的实际需要