2017-01-26 59 views
1

我想從mysql查詢創建表,但添加一個帶有時間戳的新列,其中我將捕獲創建日期。將時間戳列添加到從查詢創建的表中

這裏是我的查詢:

CREATE TABLE archive 
    AS (SELECT a.* FROM 
     (
      (
       SELECT p.projectNo, usr.staff_id, usr.longname, usr.username, p.title, CONCAT(upr.process, " (", upr.role, ")") AS process 
       FROM project p 
       INNER JOIN user_project upr ON p.projectNo = upr.projectNo 
        INNER JOIN user usr ON upr.username = usr.username 
      ) 
     ) AS a 
     ORDER BY a.projectNo); 

如何添加時間戳?

+0

這個創建列的數據來源是什麼? –

+0

@TimBiegeleisen此列應顯示創建表時的日期 –

回答

2

您可以選擇NOW()作爲創建時間戳的額外列,例如,

CREATE TABLE archive AS 
(
    SELECT a.* 
    FROM 
    (
     (
      SELECT p.projectNo, 
        usr.staff_id, 
        usr.longname, 
        usr.username, 
        p.title, 
        CONCAT(upr.process, " (", upr.role, ")") AS process, 
        NOW() AS creation_time  -- added column 
      FROM project p 
      INNER JOIN user_project upr 
       ON p.projectNo = upr.projectNo 
      INNER JOIN user usr 
       ON upr.username = usr.username 
     ) 
    ) AS a 
    ORDER BY a.projectNo 
);