mysql:
SELECT *
FROM (select * from songs order by artist asc) as songs2
WHERE artist LIKE 'a%' GROUP BY artist`
mssql:
SELECT *
FROM (select top 1000 * from songs order by artist asc) as songs2
WHERE artist LIKE 'a%' GROUP BY artist
这适用于mysql但在mssql中我收到错误:
列'songs2.id'在选择列表中无效,因为它不是 包含在聚合函数或GROUP BY子句
中
++++++++++++++++++ 现在我选择这个查询: 选择艺术家,专辑,歌曲(从歌曲顺序选择前1000名* asc,艺术家asc)作为歌曲2,其中艺术家喜欢'a%'按艺术家,专辑,歌曲分组; 但它不按艺术家分组,我希望查询中每首歌只有一位艺术家
答案 0 :(得分:1)
在mysql中,您可以选择不在group by
语句中的值。这在mssql中是不可能的。
SELECT artist FROM
(
select * from songs
) as songs2
where artist like 'a%' group by artist;
而且你也不能在子查询中使用order by。
答案 1 :(得分:0)
删除ORDER BY
子句:
SELECT * FROM
(
select * from songs
) as songs2
where artist like 'a%';
没有必要订购sub-query
中的行,因为您没有使用TOP
来获取N
,OFFSET
来应用分页或{{1订购FOR XML
个节点。
如果您想订购结果,请在外部查询中执行:
XML
所以,基本上,你可以缩短这个:
SELECT * FROM
(
select * from songs
) as songs2
where artist like 'a%'
order by year asc, artist asc;
答案 2 :(得分:0)
在子查询中排序需要 TOP / OFFSET / FOR XML Cluase
SELECT artist FROM
(
select top 100 percent * from songs
order by year asc, artist asc
) as songs2
where artist like 'a%' group by artist;
答案 3 :(得分:0)
这将为每位艺术家找到1首歌曲,最多可以找到1000首歌曲:
SELECT TOP 1000
artist,album,song
FROM
(
SELECT
artist,album,song,
row_number() over (partition by artist order by (SELECT 1)) rn
FROM table_name
WHERE artist like 'a%'
) as t2
WHERE rn = 1