0

我正在尝试加入一个选择语句。但是子查询选择返回太多行,使其非常慢 - 因为我无法从第一个选择中获取 ID 到子查询中。

错误是:

错误代码:1054。“where 子句”中的未知列“ritzau_data.News.id”

如何在第一个选择中获取此 id 到连接内的选择中?我在 MSSQL 中做了类似的事情——效果很好。我还将粘贴有效的 MSSQL 代码。

MySQLCode(不工作):

select News.id as newsid, header as title, content as contents, created as published_time, 2883 as drupalid, isins, categories, NewsTypes.npName from ritzau_data.News,
(
    select idNews, group_concat(isin separator ', ') as isins
    from ritzau_data.NewsIsin
    where idNews = ritzau_data.News.id
    group by idNews

) as isins,
(
    select idNews, group_concat(distinct Category separator ', ') as categories
    from ritzau_data.NewsTags

    inner join ritzau_data.TagsCategoriesMap on NewsTags.idTag = TagsCategoriesMap.idTags
    inner join ritzau_data.Categories on TagsCategoriesMap.idCategories = Categories.id
    where idNews = ritzau_data.News.id
    group by idNews
) as categories

left join ritzau_data.NewsTypes on News.idType = NewsTypes.id
inner join ritzau_data.NewsReceivers on News.id = NewsReceivers.idNews
where ritzau_data.News.id > 100 and idReceiver = 3
order by News.id
limit 100;

MSSQLCode(工作):

SELECT TOP 1000 news.id AS newsid, 
        cast(title as varchar(256)) AS title, cast(contents as text)AS contents, published_time, users.drupalid,
        cast (STUFF(
            (
                SELECT ',' + isin
                FROM news_instruments t1
                left join instruments t2 on t1.instrument_id = t2.id
                where news_id = news.id
                FOR XML PATH('')
            ),1,1,''
        ) as text) AS isins,
        cast(STUFF(
            (
                SELECT ',' + t2.name
                FROM news_categories t1
                left join categories t2 on t1.category_id = t2.id
                where news_id = news.id
                FOR XML PATH('')
            ),1,1,''
        ) as text) AS categories
        from news
        left join users on news.locked_userid = users.id
        where news.id > @offsetid

第一个查询 - 它有效但非常慢

select News.id as newsid, header as title, content as contents, created as published_time, 2883 as drupalid, isins, categories, NewsTypes.npName from ritzau_data.News    
left join ritzau_data.NewsTypes on News.idType = NewsTypes.id
inner join ritzau_data.NewsReceivers on News.id = NewsReceivers.idNews
select idNews, group_concat(isin separator ', ') as isins
        from ritzau_data.NewsIsin
        where idNews = News.id
        group by idNews
) as isins on News.id = isins.idNews
left join (
        select idNews, group_concat(distinct Category separator ', ') as categories
        from ritzau_data.NewsTags

        inner join ritzau_data.TagsCategoriesMap on NewsTags.idTag = TagsCategoriesMap.idTags
        inner join ritzau_data.Categories on TagsCategoriesMap.idCategories = Categories.id
        where idNews = News.id
        group by idNews
) as categories on News.id = categories.idNews
where News.id > @idoffset and idReceiver = 3
order by News.id
limit 100;
4

1 回答 1

0

问题是,新闻表在不同的模式中,因此您必须在 where 子句中提供完全限定名称:

select [...]
from ritzau_data.News
[...]
where ritzau_data.News.id > @idoffset and idReceiver = 3
order by News.id
limit 100;

优雅的方法是给表别名:

select [...]
from ritzau_data.News nw
[...]
where nw.id > @idoffset and idReceiver = 3
order by nw.id
limit 100;

更新: 您必须将子查询中的联接从子查询中拉到新闻表中:

SELECT News.id AS newsid, header AS title, content AS contents, created AS published_time, 2883 AS drupalid, isins, categories, NewsTypes.npName 
FROM ritzau_data.News ritzau_data.News
JOIN
(
    SELECT idNews, GROUP_CONCAT(isin SEPARATOR ', ') AS isins
    FROM ritzau_data.NewsIsin
    GROUP BY idNews

) AS isins
  ON isins.idNews = ritzau_data.News.id
 JOIN
(
    SELECT idNews, GROUP_CONCAT(DISTINCT Category SEPARATOR ', ') AS categories
    FROM ritzau_data.NewsTags

    INNER JOIN ritzau_data.TagsCategoriesMap ON NewsTags.idTag = TagsCategoriesMap.idTags
    INNER JOIN ritzau_data.Categories ON TagsCategoriesMap.idCategories = Categories.id
    GROUP BY idNews
) AS categories
  ON categories.idNews = ritzau_data.News.id
LEFT JOIN ritzau_data.NewsTypes ON News.idType = NewsTypes.id
INNER JOIN ritzau_data.NewsReceivers ON News.id = NewsReceivers.idNews
WHERE ritzau_data.News.id > 100 AND idReceiver = 3
ORDER BY News.id
LIMIT 100;

更新2: 将加入新闻拉到子查询中:

SELECT News.id AS newsid, header AS title, content AS contents, created AS published_time, 2883 AS drupalid, isins, categories, NewsTypes.npName 
FROM ritzau_data.News ritzau_data.News
JOIN
(
    SELECT idNews, GROUP_CONCAT(isin SEPARATOR ', ') AS isins
    FROM ritzau_data.NewsIsin
    JOIN ritzau_data.News ON idNews = ritzau_data.News.id
    GROUP BY idNews

) AS isins
 JOIN
(
    SELECT idNews, GROUP_CONCAT(DISTINCT Category SEPARATOR ', ') AS categories
    FROM ritzau_data.NewsTags

    INNER JOIN ritzau_data.TagsCategoriesMap ON NewsTags.idTag = TagsCategoriesMap.idTags
    INNER JOIN ritzau_data.Categories ON TagsCategoriesMap.idCategories = Categories.id
    JOIN ritzau_data.News ON idNews = ritzau_data.News.id
    GROUP BY idNews
) AS categories
LEFT JOIN ritzau_data.NewsTypes ON News.idType = NewsTypes.id
INNER JOIN ritzau_data.NewsReceivers ON News.id = NewsReceivers.idNews
WHERE ritzau_data.News.id > 100 AND idReceiver = 3
ORDER BY News.id
LIMIT 100;
于 2013-11-14T09:57:40.313 回答