Mysql,成功查询,但没有结果输入DB

时间:2015-06-10 00:51:21

标签: php mysql

我有一个插入查询,运行正常,但我必须从查询外的表中添加另一个字段。我想我会用另一个Join来做这个,但所有这一切都是成功运行查询并在数据库中没有显示结果。

INSERT INTO m_aa_rma_items
(
    web_rma_id,
    sku,
    reason,
    resolution,
    item_name,
    aa_rma_id
)
SELECT DISTINCT 
    m_rma_item.rma_id, 
    catalog_product_entity.sku,
    m_rma_item.reason_id, 
    m_rma_item.resolution_id, 
    m_rma_item.name,
    m_aa_rma_rma.aa_rma_id
FROM m_rma_item    
    INNER JOIN catalog_product_entity ON m_rma_item.product_id = catalog_product_entity.entity_id                        
    INNER JOIN m_rma_rma ON m_rma_rma.rma_id = m_rma_item.rma_id    
    INNER JOIN m_aa_rma_rma ON m_aa_rma_rma.aa_rma_id = m_rma_item.rma_id    
WHERE m_rma_rma.status_id= "2"

在上一个查询中,数据被输入m_aa_rma_rma表,显然我将相关项放入m_aa_rma_items表。如果取出最后一个INNER JOIN以及字段aa_rma_idm_aa_rma_rma.aa_rma_id,则此查询的功能就像它应该的那样。     我想要完成的是将相关ID(aa_rma_id)放入aa_rma_id表中的字段m_aa_rma_items。我希望我已经足够彻底和连贯到足以理解它。我在哪里遇到这个问题以实现这个目标?

2 个答案:

答案 0 :(得分:0)

问题是你正在做INNER JOIN,它只提供匹配的行。因此,如果没有匹配的行,那么最终会向目标表插入0行。如果您怀疑导致上一个JOIN问题,请将其更改为LEFT JOIN,而不是如下所示,并验证INSERT

LEFT JOIN m_aa_rma_rma
ON m_aa_rma_rma.aa_rma_id = m_rma_item.rma_id

编辑:

不确定您的桌面结构。所以稍微猜一下,试试下面修改过的查询

INSERT INTO m_aa_rma_items(         web_rma_id,
                                    sku,
                                    reason,
                                    resolution,
                                    item_name,
                                    aa_rma_id
                                )
                    SELECT DISTINCT m_rma_item.rma_id, 
                                    catalog_product_entity.sku,
                                    m_rma_item.reason_id, 
                                    m_rma_item.resolution_id, 
                                    m_rma_item.name,
                                    m_rma_item.rma_id

                    FROM m_rma_item

                    INNER JOIN catalog_product_entity
                    ON m_rma_item.product_id = catalog_product_entity.entity_id

                    INNER JOIN m_rma_rma
                    ON m_rma_rma.rma_id = m_rma_item.rma_id
                    AND m_rma_rma.status_id= "2"

                    LEFT JOIN m_aa_rma_rma
                    ON m_aa_rma_rma.aa_rma_id = m_rma_item.rma_id;

答案 1 :(得分:0)

INSERT INTO m_aa_rma_items(         web_rma_id,
                                    sku,
                                    reason,
                                    resolution,
                                    item_name,
                                    aa_rma_id
                                )
                    SELECT DISTINCT m_rma_item.rma_id, 
                                    catalog_product_entity.sku,
                                    m_rma_item.reason_id, 
                                    m_rma_item.resolution_id, 
                                    m_rma_item.name,
                                    m_aa_rma_rma.aa_rma_id

                    FROM m_rma_item

                    INNER JOIN catalog_product_entity
                    ON m_rma_item.product_id = catalog_product_entity.entity_id

                    INNER JOIN m_rma_rma
                    ON m_rma_rma.rma_id = m_rma_item.rma_id
                    AND m_rma_rma.status_id= "2"

                    LEFT JOIN m_aa_rma_rma
                    ON m_aa_rma_rma.web_rma_id = m_rma_item.rma_id;