PHP/MYSQL:Left Join 没有获得正确的父详细信息

PHP/MYSQL : Left Join not getting proper parent details

SELECT smirt.id as mid,
pc.packet_name as pname, 
pcs.packet_name as mainn, 
smirt.p_status as st , 
smirt.new_value as weight , 
smirt.packet_id, 
smirt.parent_packet_id 
FROM issue_receive_process AS smirt 
LEFT JOIN packet_creation AS pc ON  pc.id = smirt.packet_id
LEFT JOIN packet_creation AS pcs ON pcs.id = pc.parent_packet_id 
WHERE smirt.type = 53
AND smirt.main_kapan_id IN(1,2) 
ORDER BY smirt.packet_id

从上面的查询我得到以下详细信息:

但是上面是错误的

因为 pname 是我的 sub-packet mainn 是我的主数据包

实际上我需要(应该是)pname(NULL,0004)和 mainn(0003,0002)如下(下图是编辑图像):

因为 0003 , 0002 是主数据包,0004 是 0002 的子数据包 ... 0003 没有任何子数据包 ....

有什么可以帮助我的地方吗?

谢谢

更新 : 父TABLE 图片那是 (issue_receive_process AS smirt)

根据用户:KISHORE 低于查询:

SELECT smirt.id
,smirt.packet_id
,smirt.main_kapan_id
,pc.id
,pc.packet_name
,pc.parent_packet_id 
FROM issue_receive_process AS smirt 
LEFT JOIN packet_creation AS pc ON  pc.id = smirt.packet_id
WHERE pc.packet_name IN ('0002','0003','0004') 
ORDER BY smirt.packet_id

结果如下:

第二次更新 table : packet_creation 下面的详细信息根据下面的查询

SELECT *
FROM `packet_creation`
WHERE `id`
IN ( 5, 11, 9 )
LIMIT 0 , 30

我对你想要什么感到很困惑。

您拥有的SQL将在运行与您的测试数据一起给出您描述为错误的数据。它正在获取记录和数据包,然后是该数据包的父级。如果数据包没有父数据包,则返回(并且应该)为 NULL。

我唯一认为你想要的是,如果没有父记录,那么你希望子数据包 ID 作为父数据包 ID 返回,子数据包返回 NULL。

如果是这样的话:-

SELECT smirt.id as mid,
        IF(pcs.packet_name IS NULL, NULL, pc.packet_name) as pname, 
        IF(pcs.packet_name IS NULL, pc.packet_name, pcs.packet_name) as mainn, 
        smirt.p_status as st , 
        smirt.new_value as weight , 
        smirt.packet_id, 
        smirt.parent_packet_id 
FROM issue_receive_process AS smirt 
LEFT JOIN packet_creation AS pc ON  pc.id = smirt.packet_id
LEFT JOIN packet_creation AS pcs ON pcs.id = pc.parent_packet_id 
WHERE smirt.type = 53
AND smirt.main_kapan_id IN(1,2) 
ORDER BY smirt.packet_id

根据上述查询结果如下:

因此我得到的最终输出是 3,4,2,但应该是 0003,0002,0004

编辑 - 将字段转换为 char 以尝试强制使用前导零:-

SELECT smirt.id as mid,
        IF(pcs.packet_name IS NULL, NULL, CAST(pc.packet_name AS CHAR)) as pname, 
        IF(pcs.packet_name IS NULL, CAST(pc.packet_name AS CHAR), CAST(pcs.packet_name AS CHAR)) as mainn, 
        smirt.p_status as st , 
        smirt.new_value as weight , 
        smirt.packet_id, 
        smirt.parent_packet_id 
FROM issue_receive_process AS smirt 
LEFT JOIN packet_creation AS pc ON  pc.id = smirt.packet_id
LEFT JOIN packet_creation AS pcs ON pcs.id = pc.parent_packet_id 
WHERE smirt.type = 53
AND smirt.main_kapan_id IN(1,2) 
ORDER BY smirt.packet_id