MySQL返回多行 [英] Mysql return more than one row

查看:135
本文介绍了MySQL返回多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询.

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  

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

I added this subquery to the 'if statement'

 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

推荐答案

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

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:

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

这篇关于MySQL返回多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆