Select every other row as male/female from mysql table

To Get the result from 'users' table as below 

+-----+--------+-------+
| row | gender | name  |
+-----+--------+-------+
|   1 | female | Lisa  |
|   2 | male   | Greg  |
|   3 | female | Mary  |
|   4 | male   | John  |
|   5 | female | Jenny |
+-----+--------+-------+
Follow the below query
SELECT *
FROM (
    SELECT users.*,
        IF(gender=0, @mr:=@mr+1, @fr:=@fr+1) AS rank
    FROM users, (SELECT @mr:=0, @fr:=0) initvars
) tmp
ORDER BY rank ASC, gender ASC;

Comments

Popular posts from this blog

Stop video playing when Bootstrap modal is closed