gpt4 book ai didi

MySQL:处理 GROUP_CONCAT 中的条件值

转载 作者:行者123 更新时间:2023-11-29 00:12:46 25 4
gpt4 key购买 nike

我正在尝试使用 LEFT JOIN 和 GROUP_CONCAT() 检索数据。但是,在不存在值的情况下,我需要一些东西来表示缺少数据,或者每个值的标识方法,以便我可以在应用程序中对它们执行某些操作。

CONCAT_WS(0x1D,
GROUP_CONCAT(DISTINCT bcod1.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod2.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod3.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod4.value
SEPARATOR 0x1F)) AS clients_options

在这里,GROUP_CONCAT() 的四个实例与用户执行的搜索中的 INNER JOIN 或 LEFT JOIN 相关:

    INNER JOIN
bookings_clients_options bco1 ON (bco1.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod1 ON (bco1.name = 'unit_code')
AND (bco1.bookings_client_option_id = bcod1.bookings_client_option_id)
AND (bcod1.value REGEXP ('.*'))
AND (bcod1.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco2 ON (bco2.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod2 ON (bco2.name = 'creditor_ev_number')
AND (bco2.bookings_client_option_id = bcod2.bookings_client_option_id)
AND (bcod2.value REGEXP ('.*'))
AND (bcod2.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco3 ON (bco3.client_id = '3')
LEFT JOIN
bookings_clients_options_data bcod3 ON (bco3.name = 'purchase_order_number')
AND (bco3.bookings_client_option_id = bcod3.bookings_client_option_id)
AND (bcod3.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco4 ON (bco4.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod4 ON (bco4.name = 'purchase_order_booking')
AND (bco4.bookings_client_option_id = bcod4.bookings_client_option_id)
AND (bcod4.value REGEXP ('Y'))
AND (bcod4.booking_attendee_id = bookings_attendees.booking_attendee_id)

这里,bcod3.value 是结果“崩溃”的地方,因为如果没有 bcod3.value 的值但有 bcod4.value 的值,则 bcod4.value 会落入 bcod3.value 的空间。

如您所见,这些列中的每一列都有一个名称,但是已经尝试...

GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F) AS unit_code

...我从周围的 CONCAT_WS() 中得到一个错误。

我试过...

IF(bcod" . $x . ".value = '', 'QQQ', GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F))

...但这似乎没有做任何事情。

我试过...

GROUP_CONCAT(DISTINCT IF(bcod" . $x . ".value = 0, 'QQQ', bcod" . $x . ".value) SEPARATOR 0x1F)

.. 虽然它确实获取了 bcod3.value 的实例,但它也获取了一些值不是“0”的 bcod1.value 实例。

最佳答案

我不是 100% 确定我理解你的问题,但我想我理解。示例数据和预期结果值得 1000 字的解释。

如果问题是 NULL 或空白值被忽略,那么只需执行以下操作:

CONCAT_WS(0x1D,
coalesce(GROUP_CONCAT(DISTINCT bcod1.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod2.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod3.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod4.value SEPARATOR 0x1F), '')
) AS clients_options

通常认为 group_concat()concat_ws() 忽略 NULL 值是一个好处。但是,如果您想对它们做些什么,只需使用 coalesce()

关于MySQL:处理 GROUP_CONCAT 中的条件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com