2

あるテーブルからpost_id、post_year、post_desc、post_title、post_dateを取得し、post_idが等しく、is_thumbが1である別のテーブルからimg_filenameを取得しようとしています(投稿のサムネイルとしてその画像を選択しました)

これは私が運がなかった限りです:

SELECT post_id, post_year, post_desc, post_title, post_date,
FROM mjbox_posts
JOIN mjbox_images
USING (img_is_thumb)
WHERE img_is_thumb = 1
AND mjbox_images.post_id = mjbox_posts.post_id

ありがとう

4

3 に答える 3

2
SELECT  p.post_id, post_year, post_desc, post_title, post_date, img_filename
FROM    mjbox_posts p
JOIN    mjbox_images i
ON      i.post_id = p.post_id
        AND i.img_is_thumb = 1

USINGまたは、構文が必要な場合は、

SELECT  post_id, post_year, post_desc, post_title, post_date, img_filename
FROM    mjbox_posts p
JOIN    mjbox_images i
USING   (post_id)
WHERE   i.img_is_thumb = 1

違いは、最初のクエリがpost_id両方のテーブルから返されるため、エイリアスを作成する必要があることです。

于 2012-06-06T12:29:05.750 に答える
2
SELECT post_id, post_year, post_desc, post_title, post_date,
FROM mjbox_posts
INNER JOIN mjbox_images on mjbox_images.post_id = mjbox_posts.post_id
WHERE img_is_thumb = 1
于 2012-06-06T12:29:30.287 に答える
0
SELECT post_id, post_year, post_desc, post_title, post_date, if(img_is_thumb, img_filename, null)
FROM mjbox_posts a,
mjbox_images b
WHERE a.post_id= b.post_id;

img_is_thumbが1の場合はimg_filenameを返し、それ以外の場合はnullを返します。

于 2012-06-06T12:37:44.247 に答える