如何 select 表中的最新行
How to select the most recent rows from tables
我有 table1
table1
id int
myvalue text
datevalue datetime ('2016-02-23 19:25:02')
和表2
table2
id int
myvalue text
datevalue datetime ('2016-02-23 19:27:58')
我想 select table1 和 table2 的最近 5 行
我猜这是一个 ORDER BY datevalue DESC LIMIT 5 ...
但是我尝试了很多方法都没有成功!
谢谢你:)
如果您想从两个表中select。那么,
查询
select * from
(
select * from table1
union all
select * from table2
)t
order by t.datevalue desc limit 5;
两个 table 在一起:
SELECT * FROM (
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2)
ORDER BY datevalue desc LIMIT 5
并且每个 table 分开:
SELECT * FROM Table1
ORDER BY datevalue desc LIMIT 5
UNION ALL
SELECT * FROM Table2
ORDER BY datevalue desc LIMIT 5
使用 union 获取整个行集(对于两个表),检查语法是否正确(MySQL 和 Oracle 例如有不同的 'calling stuff' 方式),如果所有其他方法都失败a to_char 并在之后对其进行排序(以便您比较字符串而不是日期,这有时会很棘手)
我有 table1
table1
id int
myvalue text
datevalue datetime ('2016-02-23 19:25:02')
和表2
table2
id int
myvalue text
datevalue datetime ('2016-02-23 19:27:58')
我想 select table1 和 table2 的最近 5 行
我猜这是一个 ORDER BY datevalue DESC LIMIT 5 ...
但是我尝试了很多方法都没有成功!
谢谢你:)
如果您想从两个表中select。那么,
查询
select * from
(
select * from table1
union all
select * from table2
)t
order by t.datevalue desc limit 5;
两个 table 在一起:
SELECT * FROM (
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2)
ORDER BY datevalue desc LIMIT 5
并且每个 table 分开:
SELECT * FROM Table1
ORDER BY datevalue desc LIMIT 5
UNION ALL
SELECT * FROM Table2
ORDER BY datevalue desc LIMIT 5
使用 union 获取整个行集(对于两个表),检查语法是否正确(MySQL 和 Oracle 例如有不同的 'calling stuff' 方式),如果所有其他方法都失败a to_char 并在之后对其进行排序(以便您比较字符串而不是日期,这有时会很棘手)