我有2个查询。
public function fetchEvents(){
$events = $this->cmsdb->query("SELECT * FROM tickets_events WHERE `active` = 1", true); //true just means if only one row is returned it will be key [0] so I can loop no matter how many rows returned
if(empty($events)){
return array();
}
foreach ($events as $k => $event) {
$events[$k]['items'] = $this->cmsdb->query("SELECT *, FROM_UNIXTIME(date, '%W, %D %M %Y') as dateString
FROM tickets_items WHERE eventId = '" . intval($event['id']) . "'
AND public = 1 AND confirmed = 1 AND date > '" . time() . "'
ORDER BY date ", true);
}
return $events;
}
我已经简化了第一个查询,我将一些参数传递给函数来过滤它返回的事件,但那部分与这个问题无关。
如果我想要获取每个事件及其项目,我会遇到的问题,它正在进行大量查询(目前有691个事件)。无论如何我可以将这些查询组合在一起吗?
答案 0 :(得分:2)
您可以JOIN
将这些表格放在一起以获得相同的答案。表现也应该更好:
SELECT
i.*
,FROM_UNIXTIME(i.date, '%W, %D %M %Y') as dateString
FROM tickets_items i
JOIN tickets_events e
ON i.eventId = e.id
WHERE i.public = 1
AND i.confirmed = 1
AND e.active = 1
AND date > CURRENT_TIMESTAMP
ORDER BY date