mysql - 左连接语句中子查询的重复条目

标签 mysql left-join

我有以下 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"
        }
    ]
}

有人在这里有想法吗?我必须限制用户关注乐队的结果

最佳答案

尝试如下一次,尝试通过删除不必要的子查询并将条件从 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; 

关于mysql - 左连接语句中子查询的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956933/

相关文章:

mysql - 在 MySQL "LIMIT"中使用 "INSERT"?

php - 如何使用mysql php更改查询中的日期格式

mysql - Docker-compose 在 CircleCI 中复制 MySQL 配置时出现问题

php - 用户之间的聊天系统

Mysql group by - order by 不起作用

mysql - 如何从 MySQL 中的表达式或字符串将位插入 BIT 字段?

mysql - 使用 "is not null"的左连接查询的优化

postgresql - 我从查询 COUNT ('e.id' ) 或 COUNT(e.id) 得到不同的结果

php - 带有 LEFT JOIN 的 SELECT 返回 NULL 值

sql - 对于连接表中所有缺失的组合返回 0