查询以查找具有特定标签Joomla 3的文章ID

时间:2016-07-03 19:28:19

标签: php mysql joomla

我试图从Joomla content_item_id表中获取文章contentitem_tag_map - 其中tag_id等于一个或多个值。问题是在Joomla(3.5)中,所有标记关联都显示在不同的行中,如:

查询涉及contentitem_tag_map表中的列:


     |  content_item_id  |  tag_id  |
     --------------------------------
     |                4  |  7       |
     --------------------------------
     |                8  |  6       |
     --------------------------------
     |                9  |  5       |
     --------------------------------
     |                9  |  9       |
     --------------------------------
     |               11  |  3       |
     --------------------------------
     |               11  |  9       |
     --------------------------------
     |               12  |  3       |
     --------------------------------

我的问题是,我可以从此表中获得content_item_id - s的查询,如果它必须有两个或更多tag_id - s?

例如:

我希望得到content_item_id" 3" AND " 9" (结果应为" 11")

OR

我希望得到tag_id" 5" AND " 9" (结果应为" 9")

我目前的查询如下:


     $articleIDQuery = $db->getQuery(true);
     $articleIDQuery->select('distinct content_item_id');
     $articleIDQuery->from($db->quoteName('#__contentitem_tag_map'));
     $articleIDQuery->where($db->quoteName('tag_id')." IN (3,9)");

     $db->setQuery($articleIDQuery);
     $taggedArticleID = $db->loadObjectList();

此查询会显示content_item_id - 具有tag_id - s" 3"或" 9"而不是" 3" AND" 9"

如果有人能就如何获得想要的结果给我任何想法,我真的很感激。我几乎是一个SQL新手,所以google的答案太复杂了,因为我无法将我的想法变成一个句子。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我使用下面的查询,但速度很慢。

SELECT `jc`.*
FROM `dbname`.`joomla_content` AS `jc`
INNER JOIN `dbname`.`joomla_contentitem_tag_map` AS `jctm0` ON `jc`.`id` = `jctm0`.`content_item_id` AND `jctm0`.`tag_id` = 5
INNER JOIN `dbname`.`joomla_contentitem_tag_map` AS `jctm1` ON `jc`.`id` = `jctm1`.`content_item_id` AND `jctm1`.`tag_id` = 9
ORDER BY `jc`.`id` ASC

在查询中选择更多标记ID可以减慢它的速度。问题是查询找到所有具有5和9 id_tag的内容,但也查找具有更多标记ID的内容,以及这两个内容。您必须迭代结果并且计数是tot_tags结果是=标记您的搜索。

或者,如果您正在使用joomla项目,则可以使用JHelperTags生成查询。将要筛选的所有标记ID放在数组中,然后:

$this->query = $tagsHelper->getTagItemsQuery($idTags, null, false, 'c.core_publish_up', 'DESC', false, 'all', '1' );        
$this->db->setQuery($this->query);
$results = $this->db->loadAssocList();

生成类似这样的查询:

SELECT m.type_alias, m.content_item_id, m.core_content_id, count(m.tag_id) AS match_count, MAX(m.tag_date) as tag_date, MAX(c.core_title) AS core_title, MAX(c.core_params) AS core_params,MAX(c.core_alias) AS core_alias, MAX(c.core_body) AS core_body, MAX(c.core_state) AS core_state, MAX(c.core_access) AS core_access,MAX(c.core_metadata) AS core_metadata, MAX(c.core_created_user_id) AS core_created_user_id, MAX(c.core_created_by_alias) AS core_created_by_alias,MAX(c.core_created_time) as core_created_time, MAX(c.core_images) as core_images,CASE WHEN c.core_modified_time = '0000-00-00 00:00:00' THEN c.core_created_time ELSE c.core_modified_time END as core_modified_time,MAX(c.core_language) AS core_language, MAX(c.core_catid) AS core_catid,MAX(c.core_publish_up) AS core_publish_up, MAX(c.core_publish_down) as core_publish_down,MAX(ct.type_title) AS content_type_title, MAX(ct.router) AS router,CASE WHEN c.core_created_by_alias > ' ' THEN c.core_created_by_alias ELSE ua.name END AS author,ua.email AS author_email
FROM #__contentitem_tag_map AS m
INNER JOIN #__ucm_content AS c ON m.type_alias = c.core_type_alias AND m.core_content_id = c.core_content_id AND c.core_state IN (1) AND (c.core_publish_up = '0000-00-00 00:00:00' OR c.core_publish_up <= '2017-07-28 15:37:34')  AND (c.core_publish_down = '0000-00-00 00:00:00' OR  c.core_publish_down >= '2017-07-28 15:37:34')
INNER JOIN #__content_types AS ct ON ct.type_alias = m.type_alias
LEFT JOIN #__users AS ua ON ua.id = c.core_created_user_id
WHERE m.tag_id IN (8,83,85) AND m.type_alias IN ('com_content.article','com_contact.contact','com_newsfeeds.newsfeed','com_users.user','com_content.category','com_contact.category','com_newsfeeds.category','com_tags.tag','com_banners.banner','com_banners.category','com_banners.client','com_users.note','com_users.category','com_weblinks.weblink','com_weblinks.category') AND c.core_access IN (0,1)
GROUP BY m.type_alias, m.content_item_id, m.core_content_id
ORDER BY MAX(`c`.`core_publish_up`) DESC

如果对过滤器数据使用 much_count 值,可以在

上找到