简体   繁体   English

选择同一行中的不同列和其他列

[英]select distinct and other column in the same row

Is there an easy, non-JOIN way, to perform this task? 是否有一种简单的非JOIN方法来执行此任务?

mysql_query("select DISTINCT artist, id from ringtones where artletter='A'");

Result should be: all artist values without duplicates with corresponding id. 结果应为:所有艺术家值,没有带有相应ID的重复项。

You can get one row per artist, with all the IDs for that artist on one line by doing this: 通过执行以下操作,您可以为每位艺术家获得一行,并将该艺术家的所有ID一行一行排列:

SELECT artist, GROUP_CONCAT(id) as `IDs`
FROM ringtones
WHERE artletter='A'
GROUP BY artist;

If you want only the first id for each artist, try this: 如果只需要每个艺术家的第一个 id ,请尝试以下操作:

SELECT artist, MIN(id) as `first_id`
FROM ringtones
WHERE artletter='A'
GROUP BY artist;

(you could get the last id by using MAX instead of MIN ) (您可以使用MAX而不是MIN来获取最后一个 id

If you want the first or last row (based on id ) for that artist, you can do a groupwise max as described in that link. 如果您想要该艺术家的第一或最后一行 (基于id ),则可以按照该链接中的说明进行成组的最大值

可能您正在寻找。

select DISTINCT artist, id where id IN(select DISTINCT id from ringtones where artletter='A')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM