【问题标题】:naming the output in sql在sql中命名输出
【发布时间】:2020-04-06 17:29:35
【问题描述】:

我正在尝试命名我的查询输出,但我无法做到 这是我的代码

select countryname , datemonth
from city, payments,State,country,dt_date,rooms,customer
where year(payments.PayDate) >"2010"  
ANd Payments.CustomerID = Customer.CustomerID
And State.CountryID = Country.CountryID
AND City.StateID = State.StateID
And Customer.CityID = City.CityID
and payments.DateID = dt_date.DateID 
union 
select countryname, datemonth
from city, payments,State,country,dt_date,rooms,booking,customer
where year(booking.DateBookingMade) >= "2010"
ANd booking.CustomerID = Customer.CustomerID
And State.CountryID = Country.CountryID
AND City.StateID = State.StateID
And Customer.CityID = City.CityID
and booking.DateID = dt_date.DateID;

我想将第一个选择命名为 payments,第二个选择命名为 booking 我的查询输出是

 countryname    datemonth
    UK           12
    USA          11
    UK            6
    UK            5
    USA           2

what I want is 

countryname    datemonth    status
    UK           12           payment
    USA          11           payment 
    UK            6           booking
    UK            5           booking
    USA           2           bookimg

感谢您的帮助

【问题讨论】:

    标签: mysql sql olap


    【解决方案1】:

    为两个查询添加一个 3d 列:

    select countryname , datemonth, 'payment' as status
    .............................
    union 
    select countryname, datemonth, 'booking' as status
    .............................
    

    【讨论】:

      【解决方案2】:

      使用显式join 语法并避免逗号和大小写表达式以避免union all

      select countryname , datemonth,
             (case when year(p.paydte) > 2010 then 'payment' else 'booking' end)
      from city c inner join
           payments p
           on  . . .  inner join
           State s
           on . . . inner join
           country cnt
           on . . . inner join 
           dt_date dt
           on . . .  inner join
           rooms r
           on . . . inner join
           customer cst
           . . .
      where year(p.PayDate) >= 2010; 
      

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 2019-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        相关资源
        最近更新 更多