我试图从表格term_relationships
中选择不同的(tr.item_id)。如果它与许多标签相关,我想避免两次列出帖子。
有人能指出我正确的方向吗?
查询:
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
谢谢你们,
我试图在一个查询中完成整个过程,但是当我需要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)
..等等..
是否有更好的选择,或者这是否可以做到?
答案 0 :(得分:1)
用于选择不同记录的Sql查询。 如果您希望输出仅作为表term_relationships中的不同term_id,您可以编写以下查询
SELECT DISTINCT term_id FROM term_relationships
或
SELECT term_id FROM term_relationships GROUP BY term_id
答案 1 :(得分:0)
您应指定要检索的字段列表,不能将DISTINCT
关键字与*
一起使用。
你应该使用这样的东西来获得唯一的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
答案 2 :(得分:0)
使用您的字段名称更具体 - 使用*在技术上是不好的做法。为此,您需要删除指定标记名称的字段,并仅将其包含在WHERE
子句中。那样DISTINCT
实际上会有效。