fanfanacs 2023-08-21 18:31 采纳率: 100%
浏览 6
已结题

starrcocks concat_ws返回多个结果问题

select CONCAT_WS(',',(case when c.b_drug_addicts = '1' then '吸毒人员' else null end) 
,(case when c.b_released_fromprison = '1' then '刑满释放人员' else null end) 
,(case when c.b_social_correctional_personnel = '1' then '社区矫正人员' else null end) 
,(case when c.b_mental_patient = '1' then '精神障碍患者' else null end) 
,(case when c.b_dangerous_goods_practitioners = '1' then '危险品从业人员' else null end) 
,(case when c.b_visiting_personnel = '1' then '信访人员' else null end)
,(case when c.b_repeat_visitor = '1' then '重复上访人员' else null end) 
,(case when c.b_key_teenagers = '1' then '重点青少年' else null end) 
,(case when c.b_involved_criminal_cases = '1' then '涉刑事案件' else null end) 
,(case when c.b_involved_civil_disputes = '1' then '涉民事纠纷案件' else null end) 
,(case when c.b_evil_related_personnel = '1' then '涉邪人员' else null end) 
,(case when c.b_gambling_related = '1' then '涉赌人员' else null end) 
,(case when c.b_dishonest = '1' then '失信人员' else null end) 
,(case when c.b_stabilization = '1' then '涉稳人员' else null end) 
,(case when c.b_pyramid_sale = '1' then '传销人员' else null end) 
,(case when c.b_law_litigation_related = '1' then '涉法涉诉人员' else null end) 
,(case when b.b_disabled = '1' then '残疾人' else null end)
,(case when b.b_low_income_households = '1' then '低保户' else null end) 
,(case when b.b_people_need_rescue = '1' then '临时救助人员' else null end) 
,(case when b.b_extremely_poor_people = '1' then '特困人员' else null end)
,(case when b.b_dbbyry = '1' then '是否低保边缘人员' else null end) 
,(case when b.b_orphan = '1' then '孤儿' else null end)
,(case when b.b_disabled_soldiers = '1' then '残疾军人' else null end)
,(case when b.b_family_dependants = '1' then '烈士遗属' else null end) 
,(case when b.b_children_distress = '1' then '困境儿童' else null end)
,(case when b.b_left_behind_children_zw = '1' then '留守儿童' else null end)
,(case when b.b_low_income = '1' then '低收入人员' else null end)
,(case when b.b_have_prison_minor_child = '1' then '服刑在教未成年人子女' else null end) 
,(case when b.b_ex_servicemen = '1' then '在乡复员军人' else null end) 
,(case when b.b_lost_only_child = '1' then '失独人员' else null end)) as tag 
from dws_person as a 
left join dws_person_help_info as b
 on a.m_id_card_no = b.m_id_card_no 
 left join dws_person_control_info as c 
 on a.m_id_card_no = c.m_id_card_no 
 where a.m_id_card_no = '51082320060926567X'

这个sql代码正常来说应该只返回一个值,但是不知道为什么会传回多个结果。想知道该怎么解决。

  • 写回答

3条回答 默认 最新

  • CF2301_77400554X 2023-08-21 19:40
    关注

    引用chatgpt内容作答:
    从你提供的 SQL 查询代码来看,使用 CONCAT_WS 函数将多个字符串合并为一个字符串,然后将其命名为 tag 列。通常情况下,这个查询应该返回一行结果,其中包含一个合并后的字符串。如果你得到了多个结果,可能是由于以下一些原因导致的:

    1、数据重复: 如果在 dws_person 表、dws_person_help_info 表或 dws_person_control_info 表中存在多行匹配的数据,那么连接操作可能会导致多个结果行。

    2、表连接: 当使用左连接(LEFT JOIN)连接多个表时,如果左表中的一行与右表中的多行匹配,将会生成多行结果。

    3、数据重复或错误: 请确保在 dws_person、dws_person_help_info 和 dws_person_control_info 表中的列 m_id_card_no 是唯一的。如果不唯一,可能会导致多个匹配行。

    如果你希望只返回一行结果,你可以考虑采取以下步骤:

    1、确保数据唯一性: 确保在涉及的三个表中,列 m_id_card_no 是唯一的,以防止连接操作产生多个匹配行。

    2、检查数据重复性: 检查是否存在重复的数据行,特别是在 dws_person_help_info 和 dws_person_control_info 表中。

    3、限制返回结果: 如果你只需要一行结果,可以使用 LIMIT 1 语句来限制查询结果只返回一行。

    修改后的查询可能如下所示:

    SELECT
        CONCAT_WS(',', ... ) AS tag
    FROM
        dws_person AS a
    LEFT JOIN
        dws_person_help_info AS b ON a.m_id_card_no = b.m_id_card_no
    LEFT JOIN
        dws_person_control_info AS c ON a.m_id_card_no = c.m_id_card_no
    WHERE
        a.m_id_card_no = '51082320060926567X'
    LIMIT 1;
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月29日
  • 已采纳回答 8月21日
  • 创建了问题 8月21日