2015-06-20 58 views
0

我有以下SQL查詢:重複條目連接語句

SELECT band.id bid, band.name, bandInfo.summary, bandImage.url bandImage, 
user.id uid, user.username, userImage.url userImage 
FROM bands AS band 
INNER JOIN band_info AS bandInfo ON band.id = bandInfo.bid 
LEFT JOIN band_image AS bandImage ON band.id = bandImage.bid 
LEFT JOIN band_followers AS follower ON follower.bid = band.id 
LEFT JOIN users AS user ON user.id IN (SELECT * FROM (SELECT user.id FROM users AS user, bands AS band, band_followers AS follower WHERE user.id = follower.uid) AS temp) AND user.activated = 1 
LEFT JOIN user_info AS userInfo ON userInfo.uid = user.id 
LEFT JOIN user_image AS userImage ON user.id = userImage.uid 
WHERE (band.activated = 1) AND (bandInfo.language = 'fr') 
ORDER BY band.name 

返回我複製線。

目標是檢索所有用戶,如果存在的話,所有用戶都會遵循所有帶寬限制來執行一些分頁結果。

我需要檢索:

{ 
    "table": "Table inconnue", 
    "rows": 
    [ 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 1, 
      "username": "everytimeicob", 
      "userImage": "xxxxxxxxx" 
     }, 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 2, 
      "username": "noob", 
      "userImage": "xxxxxxxx" 
     }, 
     { 
      "bid": 2, 
      "name": "Veil Of Maya", 
      "summary": "Veil of Maya is a good band", 
      "bandImage": null, 
      "uid": 1, 
      "username": "everytimeicob", 
      "userImage": "xxxxxx" 
     } 
    ] 
} 

但我檢索:

{ 
    "table": "Table inconnue", 
    "rows": 
    [ 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 1, 
      "username": "everytimeicob", 
      "userImage": "xxxxxxxxx" 
     }, 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 1, 
      "username": "everytimeicob", 
      "userImage": "xxxxxxxxx" 
     }, 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 2, 
      "username": "noob", 
      "userImage": "xxxxxxxx" 
     }, 
     { 
      "bid": 1, 
      "name": "Children Of Bodom", 
      "summary": "Children of Bodom est un groupe de metal finlandais. Les cinq membres du groupe sont originaires d'Espoo, une ville finlandaise voisine d'Helsinki. Le groupe ne s’appelle ainsi que depuis 1997, avant quoi il s’appelait Inearthed. La mascotte du groupe est affectueusement appelée Roy et représente l'image folklorique de la mort, la faucheuse. Il est présent sur toutes les couvertures d'albums jusqu'à présent. <a href="http://www.last.fm/music/Children+of+Bodom">Read more about Children of Bodom on Last.fm</a>.", 
      "bandImage": "http://userserve-ak.last.fm/serve/500/87864171/Children+of+Bodom+band.png", 
      "uid": 2, 
      "username": "noob", 
      "userImage": "xxxxxxxxx" 
     }, 
     { 
      "bid": 2, 
      "name": "Veil Of Maya", 
      "summary": "Veil of Maya is a good band", 
      "bandImage": null, 
      "uid": 1, 
      "username": "everytimeicob", 
      "userImage": "xxxxxxxx" 
     }, 
     { 
      "bid": 2, 
      "name": "Veil Of Maya", 
      "summary": "Veil of Maya is a good band", 
      "bandImage": null, 
      "uid": 2, 
      "username": "noob", 
      "userImage": "xxxxxxx" 
     } 
    ] 
} 

這裏任何人都得到一個想法?我不得不限制用戶的結果,下面帶

回答

1

嘗試像下面一次,試圖通過消除不必要的子查詢和 WHERE子句JOIN ON條款

SELECT band.id as bid, 
band.name, 
bandInfo.summary, 
bandImage.url as bandImage, 
u.id as uid, 
u.username, 
userImage.url as userImage 
FROM bands AS band 
INNER JOIN band_info AS bandInfo ON band.id = bandInfo.bid AND bandInfo.language = 'fr' 
LEFT JOIN band_image AS bandImage ON band.id = bandImage.bid 
LEFT JOIN band_followers AS follower ON follower.bid = band.id 
LEFT JOIN (
SELECT user.id, 
user.username 
FROM users AS user 
WHERE user.activated = 1 
LIMIT 10 
) u ON u.id = follower.uid 
LEFT JOIN user_info AS userInfo ON userInfo.uid = u.id 
LEFT JOIN user_image AS userImage ON u.id = userImage.uid 
WHERE band.activated = 1 
ORDER BY band.name; 
+0

是..但如果我使用極限,它會限制對樂隊,而不是users.id在band_followers – everytimeicob

+0

不知道我的理解。如果我使用LIMIT,你的意思是?您的子查詢實際上並不需要,因爲您可以直接與bandfollower進行連接。 – Rahul

+0

我想限制'LEFT JOIN users on user.id = follower.uid AND user.activated = 1'提供的結果,因爲我想爲每個樂隊做一些分頁,例如只有10個用戶,而不是全部 – everytimeicob

0
LEFT JOIN band_followers AS follower ON follower.bid = band.id 
LEFT JOIN users AS user ON user.id IN (SELECT * FROM 
(SELECT user.id FROM users AS user, 
bands AS band, band_followers AS follower 
WHERE user.id = follower.uid) AS temp) AND user.activated = 1 

移動條件修改您的查詢您加盟太多:全部追隨者兩次,樂隊沒有理由。

如果你想「下面」的所有實例,你應該在邏輯上與相關的表開始:

SELECT * from band_followers 

然後加入樂隊和用戶表:

SELECT 
    band_followers.uid, 
    bands.id bid, bands.name, 
    user.username, 
    band_info.summary, band_image.url burl, 
    user_image.url uurl 
FROM 
    band_followers 
    INNER JOIN bands on band_followers.bid=bands.id 
    INNER JOIN band_info on band_info.bid=bands.id 
    INNER JOIN band_image on band_image.bid=bands.id 
    INNER JOIN users on users.id=band_followers.uid 
    INNER JOIN user_image on user_image.uid=band_followers.uid 

INNER JOIN假設所有領域是必須的。 最後補充條件:

SELECT 
    band_followers.uid, 
    bands.id bid, bands.name, 
    user.username, 
    band_info.summary, band_image.url burl, 
    user_image.url uurl 
FROM 
    band_followers 
    INNER JOIN bands on band_followers.bid=bands.id 
    INNER JOIN band_info on band_info.bid=bands.id 
    INNER JOIN band_image on band_image.bid=bands.id 
    INNER JOIN users on users.id=band_followers.uid 
    INNER JOIN user_image on user_image.uid=band_followers.uid 
WHERE 
    bands.activated=1 
    AND users.activated = 1 
    AND band_info.language = 'fr' 
+0

謝謝你的回答。即使沒有任何追隨者,我也不想要所有追隨者的實例,但我想限制追隨者的實例 – everytimeicob