我的查询如下:
SELECT
ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status
FROM
(
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Cust_Bill_Reg_M_Tbl
UNION ALL
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Rail_Ticket_Booking_M_Tbl
)
order by CONVERT(datetime, Reg_date, 101) desc
我收到了这个错误。
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'order'.
我的查询有什么问题?如果错了,实际方法是什么?任何帮助将不胜感激。
答案 0 :(得分:4)
您需要为派生表(tmp)设置别名:
SELECT
ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status
FROM
(
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Cust_Bill_Reg_M_Tbl
UNION ALL
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Rail_Ticket_Booking_M_Tbl
) tmp
order by CONVERT(datetime, Reg_date, 101) desc
答案 1 :(得分:1)
您需要为子查询命名,例如使用x
:
SELECT
ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status
FROM
(
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Cust_Bill_Reg_M_Tbl
UNION ALL
SELECT ID,Bill_typeID,Cust_ID,Name,Reg_date,Account_Number,Amount,status FROM Rail_Ticket_Booking_M_Tbl
) x
order by CONVERT(datetime, Reg_date, 101) desc
答案 2 :(得分:1)
您需要在子查询中添加别名,如下所示:
SELECT
ID,
Bill_typeID,
Cust_ID,
Name,
Reg_date,
Account_Number,
Amount,status
FROM
(
SELECT
ID,
Bill_typeID,
Cust_ID,Name,
Reg_date,
Account_Number,
Amount,
status
FROM Cust_Bill_Reg_M_Tbl
UNION ALL
SELECT
ID,
Bill_typeID,
Cust_ID,
Name,
Reg_date,
Account_Number,
Amount,
status
FROM Rail_Ticket_Booking_M_Tbl
) Alias
order by CONVERT(datetime, Reg_date, 101) desc