搜索

首页  >  问答  >  正文

UNION 无法将两个结果与同一列合并

在这里输入图像描述我试图将这两个查询合并在同一个显示结果中,但是Mysql系统一直说UNION不能在这个位置。如果联合不起作用,我如何组合这两个查询?

P粉647504283P粉647504283231 天前491

全部回复(1)我来回复

  • P粉329425839

    P粉3294258392024-04-03 11:58:44

    https://dev.mysql.com/doc/refman /8.0/en/union.html 说:

    就您而言,它看起来像这样:

    (select customer_id, points, state from customers where state = 'CA' order by points desc limit 3)
    union
    (select customer_id, points, state from customers where state = 'FL' order by points desc limit 3)

    您可能还想了解窗口函数

    select customer_id, points, state
    from (
      select customer_id, points, state, 
        row_number() over (partition by state order by points desc) as rownum
      from customers where state in ('CA','FL')
    ) as t
    where rownum <= 3

    回复
    0
  • 取消回复