[英]get all tags with sql query
I have this sql that is made by help of others. 我有这个由别人帮助的sql。
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
LEFT JOIN nv_images i on i.entrie_id = e.id
where t.tag in ( $tag_list )
group by e.id
having count(t.id) = $num_tags ";
The result is this (i only show one entrie here, could be more): 结果是这样的(我在这里只显示一个入口,可能更多):
[1] => Array
(
[id] => 2
[band] => Kids for Cash
[album] => No More Walls E.P.
[label] =>
[year] => 1986
[text] => Text about album kids for cash.
[entrie_id] => 2
[source] => img02_9lch1.png
[tag_list] => tree
)
For the tags, i have to show all tags that a entrie has and highlight the tags that where used to get the result. 对于标签,我必须显示一个实体拥有的所有标签,并突出显示用于获取结果的标签。 In this case
[tag_list] => tree
only shows one tag, the one that was used in the search field. 在这种情况下,
[tag_list] => tree
仅显示一个标签,即在搜索字段中使用的标签。 My question is, how can i get a result like this?: 我的问题是,我怎么能得到这样的结果?:
...
[tag_list] => tree, green, foo, bar
[used_tags] => tree
)
As a array is also good, but then please also an array when it's just one item. 作为一个数组也很好,但是当它只是一个项目时,还请一个数组。
If I understood correctly use >= in the having condition 如果我理解正确,则在条件中使用> =
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
LEFT JOIN nv_images i on i.entrie_id = e.id
JOIN nv_tags t on t.entrie_id = e.id
where t.tag in ( $tag_list )
group by e.id
having count(t.id) >= $num_tags ";
ADD 加
subquery approach: 子查询方法:
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id in (
select se.id
from nv_entries se
JOIN nv_tags st on st.entrie_id = se.id
where st.tag in ( $tag_list )
group by se.id
having count(st.id) >= $num_tags
)
LEFT JOIN nv_images i on i.entrie_id = e.id
WHERE 1
group by e.id
";
Into subquery I get the ID list of entrie havin at least requested tags, then in main query I get all infox 进入子查询,我得到至少请求标签的entrie havin的ID列表,然后在主查询中,我得到所有的infox
ADD fixed query (see asker comment) 添加固定查询(请参阅问询者评论)
subquery approach, fix the lost join between "e" and "t" : 子查询方法,修复“ e”和“ t”之间丢失的联接:
$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
LEFT JOIN nv_images i on i.entrie_id = e.id
WHERE e.id in (
select se.id
from nv_entries se
JOIN nv_tags st on st.entrie_id = se.id
where st.tag in ( $tag_list )
group by se.id
having count(st.id) >= $num_tags
)
group by e.id
";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.