首頁  >  問答  >  主體

UNION 無法將兩個結果與同一列合併

在這裡輸入圖像描述我試著將這兩個查詢合併在同一個顯示結果中,但Mysql系統一直說UNION不能在這個位置。如果聯合不起作用,我該如何組合這兩個查詢?

P粉647504283P粉647504283182 天前418

全部回覆(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
  • 取消回覆