0

我正在尝试从 table 中选择 distinct(tr.item_id) term_relationships。如果帖子与许多标签相关,我想避免两次列出帖子。

有人能指出我正确的方向吗?

查询:

tag_inject = "tr.taxonomy_id = 94 or tr.taxonomy_id = 92 or tr.taxonomy_id = 93"

SELECT * FROM term_relationships tr" + _
" INNER JOIN term_taxonomy tax ON tr.taxonomy_id = tax.taxonomy_id" + _
" INNER JOIN post po ON tr.item_id = po.post_id" + _
" where tr.cpa_id = " & cpa_id & " and (" & tag_inject & ")" + _
" and po.cpa_id = " & cpa_id & " and po.post_status = 1" + _
" order by po.post_start_date desc limit " & amount_1

我在这里回答,因为评论字段很小,我不想等 6 个小时

多谢你们,

我试图在一个查询中完成整个事情,但是当我需要不同的 item_id 时,我认为这是不可能的?

所以我决定在得到不同的项目列表后,用子查询来获取帖子数据。

有点像这样:

sql_tags = "SELECT distinct tr.item_id" + _
" FROM term_relationships tr" + _
" INNER JOIN term_taxonomy tax ON tr.taxonomy_id = tax.taxonomy_id" + _
" INNER JOIN post po ON tr.item_id = po.post_id" + _
" where tr.cpa_id = " & clng(cpa_id) & " and (" & tag_inject & ")" + _
" and po.cpa_id = " & clng(cpa_id) & " and po.post_status = 1" + _
" order by po.post_start_date desc limit " & clng(content_amount_1)
set rs_plist = conn.execute(sql_tags)

接着..

while not rs_plist.eof 

get_item_id = rs_plist("item_id")

sql = "select rec_id, post_id, post_title, post_line_desc, post_image_1, post_image_1_width from " & app_database & ".post" + _
" where cpa_id = " & clng(cpa_id) & " and post_id = " & get_item_id
set rs_post = conn.execute(sql)

..等等..

有没有更好的选择,或者这是这样做的方式?

4

3 回答 3

1

嗨,用于选择不同记录的 Sql 查询。如果您只想将输出作为与表 term_relationships 不同的 term_id 输出,您可以编写以下查询

SELECT DISTINCT term_id FROM term_relationships 

或者

SELECT term_id FROM term_relationships GROUP BY term_id 
于 2013-03-12T11:37:51.783 回答
0

您应该指定要检索的字段列表,不能使用DISTINCT关键字 with *

你应该使用这样的东西来获得唯一的 post_id

tag_inject = "tr.taxonomy_id = 94 or tr.taxonomy_id = 92 or tr.taxonomy_id = 93"

SELECT DISTINCT po.post_id FROM term_relationships tr" + _
" INNER JOIN term_taxonomy tax ON tr.taxonomy_id = tax.taxonomy_id" + _
" INNER JOIN post po ON tr.item_id = po.post_id" + _
" where tr.cpa_id = " & cpa_id & " and (" & tag_inject & ")" + _
" and po.cpa_id = " & cpa_id & " and po.post_status = 1" + _
" order by po.post_start_date desc limit " & amount_1
于 2013-03-12T11:37:37.067 回答
0

更具体地使用您的字段名称 - 使用 * 在技术上是不好的做法。为此,您需要删除指定标签名称的字段,并仅将它们包含在您的WHERE子句中。这种方式DISTINCT实际上会奏效。

于 2013-03-12T11:37:59.880 回答