mysql - JOIN 之后选择不同的值(asp 经典)

标签 mysql sql join asp-classic distinct

我正在尝试从表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/

相关文章:

php - 如何从 MySQL 数据库表中获取最后插入的 ID?

mysql group_concat 在 centos 服务器上返回垃圾整数值

c# - 如何在C#中声明sql变量

java - SQL 不同的多列

mysql - 如何使用 SQL 将相同的列值分组在一起?

php - 如果列为空,在 php 中回显会出现错误

mysql - 连接两个表然后分组

mysql - 如何在不计算原始数的情况下找到重复数

php - SQL排序多个数据行而不合并行

sql - 使用左关节时如何检查列是否没有任何值?