0

テーブルからdistinct(tr.item_id)を選択しようとしていますterm_relationships。多くのタグに関連している場合は、投稿を2回リストすることは避けたいと思います。

誰かが私を正しい方向に向けることができますか?

クエリ:

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時間待ちたくなかったのでここで答えます

みんなありがとう、

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)

..等々..

より良い代替案はありますか、それともこれがそれを行う方法ですか?

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キーワードを とともに使用することはできません*

一意の 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 に答える