我有三张桌子,
Entries
, Tags
,Entry_Tag_Link
我有我的查询,它通过并将所有内容从Entries
表中拉出
SELECT *
FROM (`blog_entries`)
ORDER BY `date` desc
我最终得到了一系列条目。
之后我循环通过
foreach($entries as &$entry)
$entry['tags'] => getTags($entry['id]);
该getTags
函数运行此查询
SELECT t.*
FROM blog_tags t
JOIN blog_entries_tags_link l
ON t.id = l.tag_id
WHERE l.entry_id = {whatever id was passed}
ORDER BY t.name asc;
所以我最终得到了一个看起来像这样的数组:
Array
(
[0] => Array
(
[id] => 1
[title] => First Title
[tags] => Array
(
[0] => Array
(
[id] => 1
[name] => t1
)
[1] => Array
(
[id] => 2
[name] => t2
)
)
)
[1] => Array
(
[id] => 2
[title] => Second Title
[tags] => Array
(
[0] => Array
(
[id] => 1
[name] => t1
)
[1] => Array
(
[id] => 3
[name] => t3
)
)
)
)
所以,这意味着如果有 200 个条目,我最终会得到 201 个查询,我想知道是否有办法在一个查询中获取我需要的所有数据(然后假设用 php 将其按摩成正确的格式) ?