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