mysql 返回所有行,MySQL返回多行

本文探讨了如何在SQL查询中使用子查询返回多列数据的问题。通过一个具体的例子,详细介绍了如何调整子查询的结构,使其能够返回两列数据,并将其整合到主查询中。此外,还提供了解决方案,包括如何使用LEFT JOIN来实现这一目标。

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'

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

解决方案

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值