我的Joomla数据库中有三个表,我想查询这些表进行合并,然后按日期排序,但是我不完全了解如何使用join function in joomla。
Table 1: __content
id , created, title, state, catid
'1' , '2010-01-01 00:00:00' , 'ContentTitle' , '1' , '23'
'2' , '2014-03-01 00:00:00' , 'ContentTitle2' , '1' , '20'
Table 2: __webcasts
id, eventdate, title, state
'1' , '2015-03-01' , 'WebcastTitle' , '1'
Table 3: __conferences
id, eventdate, title, state
'1' , '2012-05-01' , 'ConferenceTitle' , '1'
我想从内容表中选择具有catid 23的所有文章,并将其与网络广播和会议表结合,然后按日期对结果进行排序,以便获得一个如下表:
id, created-eventdate, title, state
'1' , '2015-03-01' , 'WebcastTitle' , '1'
'1' , '2012-05-01' , 'ConferenceTitle' , '1'
'1' , '2010-01-01 00:00:00' , 'ContentTitle' , '1'
我认为,更复杂的是表之间日期格式的差异。如何使用Jdatabase组合和查询我的3个表来构成查询?
最佳答案
使用以下查询,您将获得结果
$q2
->select('id , event_date As date, title, status')
->from('webcasts')
;
$q1
->select('id , event_date As date, title, status')
->from('conferences')
;
$query
->select('id , created_date As date, title, status')
->from('content')
->where('catid = 23')
->union($q1)
->union($q2)
->order('id ASC, date desc')
;
$result = $db->setQuery($query)->loadObjectList();
下面是获取结果的原始mysql查询
SELECT a.id AS id, a.created_date AS date, a.title, a.state FROM (SELECT id, created_date, title, state FROM content WHERE catid = 23) AS a
UNION ALL
SELECT id, event_date As date, title, state FROM webcasts
UNION ALL
SELECT id, event_date As date, title, state FROM conferences
order by id asc, date desc
关于php - 将3个数据库表连接在一起,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46518654/