0

これは、照会したい DB 構造の一部です。

DB構造

関係は次のとおりです。

  • article.art_author - user.usr_id
  • article.art_id - tag_article.rel_art_id
  • tag_article.rel_tag_id - tag.tag_id

可能であれば、選択したユーザーから書かれた記事 (usr_id による)、または選択されたタグ (tag_id による) を持つ記事を 1 つのクエリで選択したいと考えています。

私はこれを試しましたが、望ましい結果が得られません:

SELECT * FROM 
                ((article JOIN user on article.art_author = user.usr_id)
                JOIN
                tag_article on article.art_id = tag_article.rel_art_id)
                JOIN
                tag on tag_article.rel_tag_id = tag.tag_id
                WHERE
                article.art_lang = '$cur_lang'
                $sql_in
                ORDER BY
                article.art_date desc
                LIMIT $first_record, $range
4

1 に答える 1

1
select distinct a.art_id
from article a,
    user u,
    tag_article ta,
    tag t
where a.art_author=u.user_id
    and ta.rel_art_id = a.art_id
    and ta.rel_tag_id = t.tag_id
    and (u.usr_id in (<your selected users>) or t.tag_id in (<your selected tag>))

必要なすべての列を選択できるようにすべてのジョイントを作成しましたが、記事データのみが必要な場合は、より迅速に実行できます。

select a.art_id
    from article a,
    tag_article ta
where a.art_id=ta.rel_art_id
    and (a.art_author in (<your selected users>) or ta.rel_tag_id in (<your selected tags>))
于 2012-11-22T20:28:18.803 に答える