mysql - Can I limit by unique count of a specific column? -



mysql - Can I limit by unique count of a specific column? -

say have database of songs.

artist | song ------------------------------------- cloud cult meaning of 8 cloud cult pretty voice jason alexander today day jason alexander virgin jason alexander wasn't me powderfinger dog

what want issue query selects songs limit of 20 artists. not 20 database records, many records there songs 20 artists. in above example, if limited 2, 5 results, songs cloud cult , songs jason alexander.

is sql can do?

you need create sub-query of artists , limit sub-query number of required artists:

sql fiddle

mysql 5.5.32 schema setup:

create table table1 (`artist` varchar(15), `song` varchar(20)) ; insert table1 (`artist`, `song`) values ('cloud cult', 'the meaning of 8'), ('cloud cult', 'pretty voice'), ('jason alexander', 'today day'), ('jason alexander', 'like virgin'), ('jason alexander', 'it wasn''t me'), ('powderfinger', 'like dog') ;

query 1:

select t.* (select distinct artist table1 limit 2) inner bring together table1 t on a.artist = t.artist

results:

| artist | song | |-----------------|----------------------| | cloud cult | meaning of 8 | | cloud cult | pretty voice | | jason alexander | today day | | jason alexander | virgin | | jason alexander | wasn't me |

mysql sql database sqlite

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -