左联接中的MySQL子查询不起作用,因为其中的find_in_set

时间:2019-03-28 11:32:44

标签: mysql

我正在尝试从附件列表表中获取文件名哈希。 有产品及其产品样式(变体) 产品和产品样式都有自己的形象 得到文件名哈希后,就可以将其用作通用产品图片

问题是,某些产品无法使用查询 在下面的示例中,两个产品都应具有hashed_filename 但我无法获得它的id.329 因为style_attachment_ids和product_attachment_ids为空

但是,如果我在哪里指定m.id,它将按预期工作

.. and where m.id(327, 329)
SELECT
    m.id AS product_id,
    m.product_number AS product_number,
    Group_concat(j1.style_id) AS style_id_list,
    j2.style_attachment_ids AS style_attachment_ids,
    j3.product_attachment_ids AS product_attachment_ids,
    j4.filename_hashed AS product_generic_img 
FROM
    product_list m 
    LEFT JOIN
        (
            SELECT
                id AS style_id,
                product_id 
            FROM
                product_style_list 
            WHERE
                product_id IS NOT NULL
        )
        j1 
        ON j1.product_id = m.id 
    LEFT JOIN
        (
            SELECT
                attachment_id AS style_attachment_ids,
                page_content_id 
            FROM
                r_attachment_and_content_list 
            WHERE
                page_name = 'ProductStyle'
        )
        j2 
        ON j2.page_content_id IN 
        (
            j1.style_id 
        )
    LEFT JOIN
        (
            SELECT
                attachment_id AS product_attachment_ids,
                page_content_id 
            FROM
                r_attachment_and_content_list 
            WHERE
                page_name = 'Product'
        )
        j3 
        ON j3.page_content_id = m.id 
    LEFT JOIN
        (
            SELECT
                id,
                filename_hashed 
            FROM
                attachment_list
        )
        j4 
        ON j4.id IN 
        (
            Concat_ws(',', j3.product_attachment_ids, j2.style_attachment_ids) 
        )
WHERE
    m.product_category_id IN (25)
    AND find_in_set(3, m.f_sub_group) 

GROUP BY
    m.id LIMIT 0,
    20;

如您所见,我无法获得id.329的style_attachment_ids和product_attachment_ids以及散列值

+--------------+------------------+---------------+-------------------------+-------------------------------------------+--+
| m.product_id | m.product_number | style_id_list | j2.style_attachment_ids |           product_generic_img             |  |
+--------------+------------------+---------------+-------------------------+-------------------------------------------+--+
|          327 | BS426            | 1992,1993     |                    5066 | 5068 81f7a94013c310c70b4a021f1eb496eb.jpg |  |
|          329 | BS428            | 1996,1997     |                         |                                           |  |
+--------------+------------------+---------------+-------------------------+-------------------------------------------+--+

是否可以在where子句中不指定id的情况下解决此问题?

0 个答案:

没有答案