我有以下SQL查詢:左/右/內對MySQL的聲明加入
SELECT band.id bid, band.name, bandInfo.summary, bandImage.url bandImage,
user.username, user.online, userImage.url userImage
FROM bands AS band
INNER JOIN band_info AS bandInfo ON band.id = bandInfo.bid
LEFT JOIN band_images AS bandImage ON band.id = bandImage.bid
LEFT JOIN band_followers AS follower ON follower.bid = band.id
RIGHT JOIN users AS user ON user.id = follower.uid
INNER JOIN user_info AS userInfo ON userInfo.uid = user.id
LEFT JOIN user_images AS userImage ON user.id = userImage.uid
WHERE ((band.activated = 1) AND (user.activated = 1)) AND (bandInfo.language = 'fr')
ORDER BY band.name
「用戶」表包含所有用戶(一行=一個用戶)
「USER_INFO」表包含所有關於用戶的信息(一行=一個用戶)
「user_images」表包含有關用戶的所有圖像(一行=一個用戶)
「樂隊」表包含了所有的樂隊(一行=一個波段)
「band_info」表包含有關一個頻帶中的所有信息(多行用於一個頻帶由於語言)
「band_images」表包含有關頻帶中的所有圖像(一行=一個頻帶)
「 band_followers'表包含用戶和樂隊之間的所有關係(一行=一個關係,所以有很多行包含相同的樂隊ID但不是相同的用戶ID)
我想檢索所有樂隊與他們的信息(INNER JOIN )即使樂隊ID不在band_followers表中。即使他沒有任何圖像,我也想通過用戶的信息(INNER JOIN)檢索樂隊ID在band_followers表中帶有INNER JOIN的用戶的信息(INNER JOIN)。
我的問題是就在JOIN關鍵字..我真的不知道,如果我要使用LEFT或RIGHT JOIN
謝謝!
更新應答:
SELECT band.id bid, band.name, bandInfo.summary, bandImage.url bandImage, user.username, user.online, userImage.url userImage
FROM bands AS band
INNER JOIN band_info AS bandInfo ON band.id = bandInfo.bid
LEFT JOIN band_images AS bandImage ON band.id = bandImage.bid
LEFT JOIN band_followers AS follower ON band.id = follower.bid
LEFT JOIN users AS user ON user.id = follower.uid AND user.activated = 1
LEFT JOIN user_info AS userInfo ON userInfo.uid = user.id
LEFT JOIN user_images AS userImage ON user.id = userImage.uid
WHERE (band.activated = 1) AND (bandInfo.language = 'fr')
ORDER BY band.name
謝謝大家
這是正確的答案,謝謝! – everytimeicob