【问题标题】:mysql form subarray(array aggregation) and returnmysql形成子数组(数组聚合)并返回
【发布时间】:2018-12-06 05:35:31
【问题描述】:

我有以下 mysql 查询

    select  m.flat_id,
            f.flat_no,
            fo.owner_name,
            oc.occupant_name,
            b.block_name,
            f.floor_no,
            f.floor_id,
            m.user_id as user_map_id

    from svk_apt_flats_users_mapping m
    left join svk_apt_users u on u.user_id = m.user_id and u.association_id = 2 
        and u.customer_id = 2 and u.is_active = 1 and u.user_id is not null
    left join svk_apt_flats f on f.flat_id = m.flat_id and f.is_active = 1
    left join svk_apt_blocks b on b.block_id = f.block_id and b.is_active = 1
    left join svk_apt_flat_owners fo on u.user_role_type_id = 4 and fo.is_active = 1 and fo.user_id = m.user_id
    left join svk_apt_occupants oc on u.user_role_type_id = 5 and oc.is_active = 1 and oc.user_id = m.user_id 

where   f.block_id = 3
        and m.user_id <> 2
        and m.customer_id = 2
        and m.association_id = 2 
        and m.is_active = 1
        and m.is_active = 1
        and (case when m.user_role_type_id = 5 then true else m.is_approved = 1 end)

和输出:

我想将具有相同 'user_map_id' 的行聚合为一行。 mysql有什么解决方案吗?

【问题讨论】:

  • 您还应该告诉我们您期望的输出,因为这可能会决定查询的内容。顺便说一句,您不需要发布如此复杂的查询;一个简化的版本就可以了,只要它可以传达你的观点。
  • 预期输出是“显示重复的 'user_map_id' 并且它是一行中的行”。
  • 如果您聚合或分组 user_map_id,您希望在查询中返回哪些其他列?就像你需要一些列或连接的总和?或其他任何东西。
  • 是的,我需要连接子数组中具有相同 user_map_id 的行,就像“Postgresql 中的array_agg() 函数”一样

标签: mysql arrays aggregate sub-array


【解决方案1】:

使用分组方式

    select  m.flat_id,
            f.flat_no,
            fo.owner_name,
            oc.occupant_name,
            b.block_name,
            f.floor_no,
            f.floor_id,
            m.user_id as user_map_id

    from svk_apt_flats_users_mapping m
    left join svk_apt_users u on u.user_id = m.user_id and u.association_id = 2 
        and u.customer_id = 2 and u.is_active = 1 and u.user_id is not null
    left join svk_apt_flats f on f.flat_id = m.flat_id and f.is_active = 1
    left join svk_apt_blocks b on b.block_id = f.block_id and b.is_active = 1
    left join svk_apt_flat_owners fo on u.user_role_type_id = 4 and fo.is_active = 1 and fo.user_id = m.user_id
    left join svk_apt_occupants oc on u.user_role_type_id = 5 and oc.is_active = 1 and oc.user_id = m.user_id 

where   f.block_id = 3
        and m.user_id <> 2
        and m.customer_id = 2
        and m.association_id = 2 
        and m.is_active = 1
        and m.is_active = 1
        and (case when m.user_role_type_id = 5 then true else m.is_approved = 1 end)
group by u.user_role_type_id;

如果您没有在它们上指定 SUM、AVG 等聚合函数,MySQL 将为所有列选择第一行的值

【讨论】:

  • 它不工作。请注意 select 语句中重复 3 次的“m.user_id as user_map_id”列。我想将重复的“user_id”与行合并为一行。
  • 如果你使用 group by m.user_id 或 group by user_map_id 会发生什么?
  • group by m.user_id 删除重复的 user_id 和它的行。但我想将这些行连接成一行以形成子数组。
  • 抱歉,我不明白你期望的输出。你能以表格的形式添加预期的输出吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 2012-03-03
相关资源
最近更新 更多