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代码正常来说应该只返回一个值,但是不知道为什么会传回多个结果。想知道该怎么解决。

引用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;


你的关联数据有没有多对多呀,你检查一下,不知道你的表结构也不知道你的具体数据,只能猜测是可能出现了多对多的匹配。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

【以下回答由 GPT 生成】

我可以理解你的困惑。在C++中,使用concat_ws函数返回多个结果的问题,可能是由于对特殊字符的处理不当导致的。为了正确使用concat_ws函数并在C++中返回正确的结果,你可以按照以下步骤进行。

步骤 1: 处理SQL查询语句。

首先,你需要将SQL查询语句存储在一个C++字符串中。在C++中,字符串的表示使用双引号(")而不是单引号(')。确保查询语句的所有引号都是双引号。

std::string query = "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) "
    ") AS concatenated_results "
    "FROM table_name c;";

在这个例子中,我将查询语句存储在名为query的C++字符串中。

步骤 2: 执行SQL查询。

接下来,你需要确定你在C++中使用的数据库类型和连接方式。这里我以MySQL数据库为例,并使用MySQL Connector/C++库来连接数据库和执行查询。你需要根据你所使用的数据库类型和库进行相应的调整。

#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>

// ...

sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance();
std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "username", "password"));

std::unique_ptr<sql::Statement> stmt(con->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(query));

在这个例子中,我使用了MySQL Connector/C++库来创建一个MySQL的连接和语句对象,并使用executeQuery方法执行查询。请确保你正确设置数据库的连接地址、用户名和密码。

步骤 3: 处理查询结果。

一旦查询执行成功,你可以使用C++代码来处理查询结果。在这个例子中,我使用了getString方法来获取查询结果中concatenated_results字段的值。

if (res->next()) {
    std::string concatenated_results = res->getString("concatenated_results");

    // 处理结果
    // ...
}

在这个例子中,我使用了getString方法来获取查询结果中concatenated_results字段的字符串值并存储在名为concatenated_results的C++字符串中。

现在,你可以根据需求对结果进行处理,比如输出到控制台、存储到文件或者进行其他操作。

这就是使用concat_ws函数在C++中返回多个结果的具体解决方案。希望对你有帮助!如果你有任何进一步的问题,请随时提问。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^