在select query

时间:2016-05-16 14:25:18

标签: mysql sql

我已按如下方式提取了两列checkin_datebooking_time。如何才能获得由此创建的两列的差异?

SELECT CAST(CONCAT(MID(Checkin,7,4) , '-' 
  , MID(Checkin,1,2)  , '-' 
  , MID(Checkin,4,2))
 AS DATETIME) as Checkin_Date,

 CAST(CONCAT(MID(Book_Time,7,4) , '-' 
  , MID(Book_Time,1,2)  , '-' 
  , MID(Book_Time,4,2))
 AS DATETIME) as Booking_Date

FROM rl_transactional.mydb

我尝试在Checkin_Date - Booking_Date之前执行FROM rl_transactional.mydb,但我收到了错误

  

错误代码:1064。您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第16行的'Checkin_Date - Booking_Date FROM rl_transactional.mydb'附近使用正确的语法

修改:根据建议,我使用了以下查询

SELECT CAST(CONCAT(MID(Checkin,7,4) , '-' 
  , MID(Checkin,1,2)  , '-' 
  , MID(Checkin,4,2))
 AS DATETIME) as Checkin_Date, 

CAST(CONCAT(MID(CheckOut,7,4) , '-' 
  , MID(CheckOut,1,2)  , '-' 
  , MID(CheckOut,4,2))
 AS DATETIME) as CheckOut_Date,

 CAST(CONCAT(MID(Book_Time,7,4) , '-' 
  , MID(Book_Time,1,2)  , '-' 
  , MID(Book_Time,4,2))
 AS DATETIME) as Booking_Date,

Checkin_Date - Booking_Date
from (
    SELECT CAST(CONCAT(MID(Checkin,7,4) , '-' 
      , MID(Checkin,1,2)  , '-' 
      , MID(Checkin,4,2))
     AS DATETIME) as Checkin_Date,
     CAST(CONCAT(MID(Book_Time,7,4) , '-' 
      , MID(Book_Time,1,2)  , '-' 
      , MID(Book_Time,4,2))
     AS DATETIME) as Booking_Date
    FROM rl_transactional.mydb
) c,
FROM rl_transactional.mydb

我收到以下错误

  

错误代码:1064。您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在'Checkin_Date - Booking_Date from(SELECT。)附近使用的语法   CAST(CONCAT(MID(Checkin,7,4),' - ''在第1行

2 个答案:

答案 0 :(得分:1)

尝试运行以下查询:

SELECT 
     c.Checkin_Date, 
     c.CheckOut_Date
     c.Booking_Date,
     c.Checkin_Date - c.Booking_Date
from (
    SELECT CAST(CONCAT(MID(Checkin,7,4) , '-' 
      , MID(Checkin,1,2)  , '-' 
      , MID(Checkin,4,2))
     AS DATETIME) as Checkin_Date,
     CAST(CONCAT(MID(Book_Time,7,4) , '-' 
      , MID(Book_Time,1,2)  , '-' 
      , MID(Book_Time,4,2))
     AS DATETIME) as Booking_Date,
     CAST(CONCAT(MID(CheckOut,7,4) , '-' 
      , MID(CheckOut,1,2)  , '-' 
      , MID(CheckOut,4,2))
     AS DATETIME) as CheckOut_Date
    FROM rl_transactional.mydb
) c;

答案 1 :(得分:1)

如果您想使用别名,则应填充查询:

select Checkin_Date - Booking_Date from (
    SELECT CAST(CONCAT(MID(Checkin,7,4) , '-'
      , MID(Checkin,1,2)  , '-' 
      , MID(Checkin,4,2))
     AS DATETIME) as Checkin_Date,
     CAST(CONCAT(MID(Book_Time,7,4) , '-' 
      ,MID(Book_Time,1,2)  , '-' 
      , MID(Book_Time,4,2))
     AS DATETIME) as Booking_Date
    FROM rl_transactional.mydb ) 

修改

您无法在查询中重复使用别名:

试试这个:

SELECT 

 Checkin_Date, 

CAST(CONCAT(MID(CheckOut,7,4) , '-' 
  , MID(CheckOut,1,2)  , '-' 
  , MID(CheckOut,4,2))
 AS DATETIME) as CheckOut_Date,

Booking_Date,

Checkin_Date - Booking_Date as Diff

from (
    SELECT CAST(CONCAT(MID(Checkin,7,4) , '-' 
      , MID(Checkin,1,2)  , '-' 
      , MID(Checkin,4,2))
     AS DATETIME) as Checkin_Date,
     CAST(CONCAT(MID(Book_Time,7,4) , '-' 
      , MID(Book_Time,1,2)  , '-' 
      , MID(Book_Time,4,2))
     AS DATETIME) as Booking_Date,
     CheckOut
    FROM rl_transactional.mydb
) c