私はフォーラムに取り組んでおり、すべてのフォーラムと同様に、スレッドと応答があります。これらは、データベースでそれらを保存するために使用するテーブルです。
post
------------
id
user_id
time
type
effective_id
thread
------------
id
update_time
text
response_number
deleted
response
------------
id
container_id
text
deleted
はでpost.type
ありenum('thread', 'response')
、は が示す内容に従って、またはpost.effective_id
と一致する必要があります。thread.id
response.id
post.type
ご覧のとおり、スレッドと応答に共通するすべてのものを因数分解しています。
私が遭遇した問題は次のとおりです。フィールドを投稿テーブルに移動せずに、単一のクエリで特定の投稿が削除されたかどうかを判断したい(id
情報として持っている) 。deleted
これらは、指定された ID がスレッドまたは応答に属しているかどうかが事前にわかっている場合に使用するクエリです。
SELECT thread.deleted
FROM post INNER JOIN thread ON post.effective_id = thread.id
また
SELECT response.deleted
FROM post INNER JOIN response ON post.effective_id = response.id
しかし、SQL では " " とはどのように言えますif the post.type is thread then INNER JOIN with thread and get the deleted field, if post.type is response then INNER JOIN with response and get the delete field.
か? ある種の動的な「if」が必要です
これは指定された条件で行うことさえ可能ですか?
ありがとう!