Can I use a single MySQL query to select distinct rows and then non-distinct rows if a limit hasn't

Posted by Matt Rix on Stack Overflow See other posts from Stack Overflow or by Matt Rix
Published on 2010-06-01T04:11:49Z Indexed on 2010/06/01 4:13 UTC
Read the original article Hit count: 208

Filed under:
|
|
|

I hope I'm explaining this properly, my knowledge of MySQL is quite limited.

Let's say I have a table with rows that have name and shape fields.

I'd like to select a bunch of rows from a table, but return all of the rows with unique shape field values first. If I have less than a certain number of rows, let's say 7, then I'd like to fill the remaining result rows with non-unique shape rows. The best way I can word it is that they're "ordered by uniqueness, and then by some other value".

So, I don't want: square, square, circle, circle, rectangle, square, triangle

I'd like to have: square, circle, rectangle, triangle, square, square, circle

Is this possible to do using a single SQL query? I'm using MySQL with PHP, if that makes any difference.

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about sql