有下列的資料表,需要抓取課程ID及其修改日期時間 ,但最後我僅需要抓取每個ID,最後的修改時間(即課程單元的修改日期資訊存在多個資料表),有甚麼語法可以將其合併在一起後,取出它最後的更新時間呢?(有想到union all, 但是它要欄位名稱都依樣)
SELECT course, FROM_UNIXTIME( timemodified ) FROM mdl_assign
SELECT id, FROM_UNIXTIME( timemodified ) FROM mdl_course
select course,FROM_UNIXTIME( timemodified ) FROM mdl_feedback
select course,FROM_UNIXTIME( timemodified ) FROM mdl_folder
SELECT courseid, FROM_UNIXTIME( timemodified ) FROM mdl_event
select course,,FROM_UNIXTIME( timemodified ) from mdl_quiz
可以用 union all .
union all 的規定是:
1.二個select 的"欄位數"要一樣多.(不可以一個select 是5個欄位,另一個select 不是5個欄位)
2.每個欄的資料型態要一致.
測試後,它是會抓出每個id (course / courseid /id) 需要在美個資料表都有存在,而我目前是希望抓取課程ID後,它可以來判斷那個資料表的修改日期是最新的,取該筆資料出來,要如何來做呢?謝謝
SELECT course, max(FROM_UNIXTIME( timemodified )) FROM mdl_assign
union all
SELECT id,max(FROM_UNIXTIME( timemodified )) FROM mdl_course
union all
select course,max(FROM_UNIXTIME( timemodified )) FROM mdl_feedback
union all
select course,max(FROM_UNIXTIME( timemodified )) FROM mdl_folder
union all
SELECT courseid, max(FROM_UNIXTIME( timemodified )) FROM mdl_event
union all
select course,max(FROM_UNIXTIME( timemodified )) from mdl_quiz
不知道這些TABLE 的意義及狀況.
有一個方式:從下的SELECT 式中.再取 您所要的資料.
SELECT * FROM
(
SELECT course, max(FROM_UNIXTIME( timemodified )) CREATE_DATE FROM mdl_assign
union all
SELECT id,max(FROM_UNIXTIME( timemodified )) CREATE_DATE FROM mdl_course
union all
select course,max(FROM_UNIXTIME( timemodified )) CREATE_DATE FROM mdl_feedback
union all
select course,max(FROM_UNIXTIME( timemodified )) CREATE_DATE FROM mdl_folder
union all
SELECT courseid, max(FROM_UNIXTIME( timemodified )) CREATE_DATE FROM mdl_event
union all
select course,max(FROM_UNIXTIME( timemodified )) CREATE_DATE from mdl_quiz
)