我可以将此语句写得短些吗?

Select S_name
from
  Supplier
  JOIN Nation ON Supplier.S_nationkey = Nation.N_nationkey
  JOIN Region on Nation.n_regionkey = region.R_regionkey
Where
  Region.r_name = 'AFRICA'
Union
Select C_name
from
  Customer
  JOIN Nation ON Customer.C_Nationkey = Nation.N_nationkey
  JOIN Region on Nation.N_regionkey = Region.R_regionkey
Where
  Region.R_name = 'AFRICA'

并且我想按名称排序我的输出,但是我不知道为什么,因为我确实有C_name和S_Name作为输出?

谢谢

最佳答案

如果您希望所有数据都在同一列中,则可以在其周围放置一个SELECT,然后执行ORDER BY

    Select S_name As Names
    from Supplier
    JOIN Nation
        ON Supplier.S_nationkey = Nation.N_nationkey
    JOIN Region
        on Nation.n_regionkey = region.R_regionkey
    Where Region.r_name = 'AFRICA'
    Union
    Select C_name As Names
    from Customer
    JOIN Nation
        ON Customer.C_Nationkey = Nation.N_nationkey
    JOIN Region
        on Nation.N_regionkey = Region.R_regionkey
    Where Region.R_name = 'AFRICA'
    ORDER BY Names

如果您不需要同一列中的数据,则可以通过以下方式进行操作:
Select S_name, c.C_name
from Supplier
JOIN Nation
    ON Supplier.S_nationkey = Nation.N_nationkey
JOIN Region
    on Nation.n_regionkey = region.R_regionkey
JOIN Customer c
    on Nation.N_nationkey = c.C_Nationkey
Where Region.r_name = 'AFRICA'
ORDER BY S_name, c.c_name

关于sql - 此语句较短/顺序由,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10281794/

10-11 05:51