简体   繁体   中英

select the nearest date in mysql using codeigniter from two different tables?

These are my two different tables .. 1) seminar

研讨会桌

2) workshop

车间桌

Need to show the nearest event by using the column posted_datetime(timestamp).. how to get only one record which is nearest to current date using codeigiter ?

Maybe try a union with a return limit of 1.

select 1, * from seminar
UNION
select 2, * from workshop
ORDER BY posted_datetime DESC
LIMIT 1

I put the leading 1 and 2 in the first column so you can tell if the returned record is a workshop (1) or seminar (2). This whole thing assumes all the fields names are the same in both tables and the bigger assumption, that my untested sql will actually work. =)

this query will return you upcoming nearest seminar

select * from seminar where posted_datetime < now() order by posted_datetime desc limit 1

In CI

$this->db->where('posted_datetime < now()');
$this->db->order_by('posted_datetime','desc');
$this->db->limit(1);
$this->db->get('seminar');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM