我正在尝试从表term_relationships
中选择distinct(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
<小时/>
我在这里回答,因为评论区太小,我不想等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)
..等等..
是否有更好的选择,或者这是这样做的方法?
最佳答案
用于选择不同记录的 Hi Sql 查询。 如果您希望输出仅与表 term_relationships 中不同的 term_id,您可以编写以下查询
SELECT DISTINCT term_id FROM term_relationships
或
SELECT term_id FROM term_relationships GROUP BY term_id
关于mysql - JOIN 之后选择不同的值(asp 经典),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359879/