Mysql返回多行

时间:2022-08-23 10:13:57

I have this query.

我有这个问题。

SELECT notes.id,enter.name as 'enter_name',step.title as 'flow status',notes.user_name as user_created,notes.created,notes.rel_client_id,td_doc_nr.value_string as 'document number',enter.enter_code,
    IF(!ISNULL(td_doc_nr.value_string),
     (SELECT  GROUP_CONCAT(product_name SEPARATOR ',') from notes d 
        join note_bundles b on b.note_id = d.id
        join note_products p on p.doc_bundle_id = b.id
        join note_product_get_fields f on f.doc_product_id = p.id
        join note_product_get_field_data fd on fd.get_field_id = f.id
        where d.doc_nr = td_doc_nr.value_string
        and value_string ='auto')
,NULL) as test
FROM notes notes
JOIN notes_steps step ON  step.id  = notes.step_id 
JOIN notes_enters enter ON enter.id = notes.enter_id
    LEFT JOIN notes_custom_fields tf_doc_nr ON tf_doc_nr.name = 'note_number' AND tf_doc_nr.rel_entity_id = enter.id
    LEFT JOIN notes_custom_field_data td_doc_nr ON td_doc_nr.rel_entity_id = notes.id AND
 td_doc_nr.field_instance_id = tf_doc_nr.id 
WHERE notes.enter_id in  (777) AND notes.status = 1  

I added this subquery to the 'if statement'

我将此子查询添加到'if语句'

 SELECT  GROUP_CONCAT(product_name SEPARATOR ',') from nontes d 
    join note_bundles b on b.note_id = d.id
    join note_products p on p.doc_bundle_id = b.id
    join note_product_get_fields f on f.doc_product_id = p.id
    join note_product_get_field_data fd on fd.get_field_id = f.id
    where d.doc_nr = 'G7777777'
    and value_string ='auto'

After this I added a new column.

在此之后我添加了一个新专栏。

 SELECT  GROUP_CONCAT(product_name SEPARATOR ','),GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') from notes d 
    join note_bundles b on b.note_id = d.id
    join note_products p on p.doc_bundle_id = b.id
    join note_product_get_fields f on f.doc_product_id = p.id
    join note_product_get_field_data fd on fd.get_field_id = f.id
    where d.doc_nr = 'G7777777'
    and value_string ='auto'

It returns two columns. How can I return two columns?Is it possible? :) Thanks

它返回两列。如何返回两列?有可能吗? :) 谢谢

2 个解决方案

#1


6  

A subquery inside an IF statement can't return multiple columns. You will need to join the subquery into the results, and pull out the two separate columns individually:

IF语句中的子查询不能返回多个列。您需要将子查询加入到结果中,并分别拉出两个单独的列:

SELECT ...
    IF(!ISNULL(td_doc_nr.value_string), sub.one, NULL) as one,
    IF(!ISNULL(td_doc_nr.value_string), sub.two, NULL) as two
FROM ...
LEFT JOIN (
    SELECT  d.doc_nr, GROUP_CONCAT(product_name SEPARATOR ','),GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') from documents d 
    join document_bundles b on b.document_id = d.id
    join document_products p on p.doc_bundle_id = b.id
    join document_product_cstm_fields f on f.doc_product_id = p.id
    join document_product_cstm_field_data fd on fd.cstm_field_id = f.id
    where value_string ='auto'
    group by d.doc_nr
) sub on sub.doc_nr = td_doc_nr.value_string

#2


2  

A correlated subquery inside an IF statement can only return 1 column and 1 row, this is why you are getting the error. However, looking over your query the only outer reference inside the subquery is

IF语句中的相关子查询只能返回1列和1行,这就是您收到错误的原因。但是,查看查询时,子查询中唯一的外部引用是

d.doc_nr = td_doc_nr.value_string

So you do not need actually need a correlated subquery and you can achieve the same result by moving the subquery to a join and grouping by doc_nr within the subquery, which will probably be much more efficient, and it will allow you to return the 2 columns you want:

因此,您不需要实际需要相关子查询,并且可以通过将子查询移动到子查询中的doc_nr的连接和分组来实现相同的结果,这可能会更高效,并且它将允许您返回2列你要:

SELECT  tickets.id,
        source.name as 'source_name',
        flow_stage.title as 'flow status',
        tickets.user_name as user_created,
        tickets.created,
        tickets.rel_client_id,
        td_doc_nr.value_string as 'document number',
        source.source_code,
        IF(!ISNULL(td_doc_nr.value_string), ProductNames, NULL) as test,
        d.MSISDNS
FROM    tickets tickets
        JOIN tickets_flow_stages flow_stage 
            ON flow_stage.id  = tickets.flow_stage_id 
        JOIN tickets_sources source 
            ON source.id = tickets.source_id
        LEFT JOIN tickets_custom_fields tf_doc_nr 
            ON tf_doc_nr.name = 'document_number' 
            AND tf_doc_nr.rel_entity_id = source.id
        LEFT JOIN tickets_custom_field_data td_doc_nr 
            ON td_doc_nr.rel_entity_id = tickets.id 
            AND td_doc_nr.field_instance_id = tf_doc_nr.id 
        LEFT JOIN
        (   SELECT  d.Doc_nr,
                    GROUP_CONCAT(product_name SEPARATOR ',') AS ProductNames,
                    GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') AS MSISDNS
            from    documents d 
                    INNER JOIN document_bundles b 
                        ON b.document_id = d.id
                    INNER JOIN document_products p 
                        ON p.doc_bundle_id = b.id
                    INNER JOIN document_product_cstm_fields f 
                        ON f.doc_product_id = p.id
                    INNER JOIN document_product_cstm_field_data fd 
                        ON fd.cstm_field_id = f.id
            WHERE   value_string ='auto'
            GROUP BY d.Doc_nr
        ) d
            ON d.doc_nr = td_doc_nr.value_string
WHERE   tickets.source_id IN (114,122,125,129,131) 
AND     tickets.status = 1 

#1


6  

A subquery inside an IF statement can't return multiple columns. You will need to join the subquery into the results, and pull out the two separate columns individually:

IF语句中的子查询不能返回多个列。您需要将子查询加入到结果中,并分别拉出两个单独的列:

SELECT ...
    IF(!ISNULL(td_doc_nr.value_string), sub.one, NULL) as one,
    IF(!ISNULL(td_doc_nr.value_string), sub.two, NULL) as two
FROM ...
LEFT JOIN (
    SELECT  d.doc_nr, GROUP_CONCAT(product_name SEPARATOR ','),GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') from documents d 
    join document_bundles b on b.document_id = d.id
    join document_products p on p.doc_bundle_id = b.id
    join document_product_cstm_fields f on f.doc_product_id = p.id
    join document_product_cstm_field_data fd on fd.cstm_field_id = f.id
    where value_string ='auto'
    group by d.doc_nr
) sub on sub.doc_nr = td_doc_nr.value_string

#2


2  

A correlated subquery inside an IF statement can only return 1 column and 1 row, this is why you are getting the error. However, looking over your query the only outer reference inside the subquery is

IF语句中的相关子查询只能返回1列和1行,这就是您收到错误的原因。但是,查看查询时,子查询中唯一的外部引用是

d.doc_nr = td_doc_nr.value_string

So you do not need actually need a correlated subquery and you can achieve the same result by moving the subquery to a join and grouping by doc_nr within the subquery, which will probably be much more efficient, and it will allow you to return the 2 columns you want:

因此,您不需要实际需要相关子查询,并且可以通过将子查询移动到子查询中的doc_nr的连接和分组来实现相同的结果,这可能会更高效,并且它将允许您返回2列你要:

SELECT  tickets.id,
        source.name as 'source_name',
        flow_stage.title as 'flow status',
        tickets.user_name as user_created,
        tickets.created,
        tickets.rel_client_id,
        td_doc_nr.value_string as 'document number',
        source.source_code,
        IF(!ISNULL(td_doc_nr.value_string), ProductNames, NULL) as test,
        d.MSISDNS
FROM    tickets tickets
        JOIN tickets_flow_stages flow_stage 
            ON flow_stage.id  = tickets.flow_stage_id 
        JOIN tickets_sources source 
            ON source.id = tickets.source_id
        LEFT JOIN tickets_custom_fields tf_doc_nr 
            ON tf_doc_nr.name = 'document_number' 
            AND tf_doc_nr.rel_entity_id = source.id
        LEFT JOIN tickets_custom_field_data td_doc_nr 
            ON td_doc_nr.rel_entity_id = tickets.id 
            AND td_doc_nr.field_instance_id = tf_doc_nr.id 
        LEFT JOIN
        (   SELECT  d.Doc_nr,
                    GROUP_CONCAT(product_name SEPARATOR ',') AS ProductNames,
                    GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') AS MSISDNS
            from    documents d 
                    INNER JOIN document_bundles b 
                        ON b.document_id = d.id
                    INNER JOIN document_products p 
                        ON p.doc_bundle_id = b.id
                    INNER JOIN document_product_cstm_fields f 
                        ON f.doc_product_id = p.id
                    INNER JOIN document_product_cstm_field_data fd 
                        ON fd.cstm_field_id = f.id
            WHERE   value_string ='auto'
            GROUP BY d.Doc_nr
        ) d
            ON d.doc_nr = td_doc_nr.value_string
WHERE   tickets.source_id IN (114,122,125,129,131) 
AND     tickets.status = 1