if mulitiple values is there like
email
ravi@gmail.com
karthi@gmail.com
ravi@gmail.com
ravi@gmail.com
karthi@gmail.com
karthi@gmail.com
and i want to diplay
ravi@gmail.com
karthi@gmail.com
ravi@gmail.com
karthi@gmail.com
ravi@gmail.com
karthi@gmail.com
如果两个值在列中混排。然后我想要一个结果一个值和另一个值。表中重复了两个值。
答案 0 :(得分:0)
您可以使用变量来创建中间结果集,如下所示:
SELECT email,
@seq := IF(@m = email, @seq + 1,
IF(@m := email, 1, 1)) AS seq
FROM mytable
CROSS JOIN (SELECT @seq :=1, @m :='') AS vars
ORDER BY email
输出:
email seq
------------------------
karthi@gmail.com 1
karthi@gmail.com 2
karthi@gmail.com 3
ravi@gmail.com 1
ravi@gmail.com 2
ravi@gmail.com 3
您现在可以在外部查询中实现所需的排序,如下所示:
SELECT email
FROM (
SELECT email,
@seq := IF(@m = email, @seq + 1,
IF(@m := email, 1, 1)) AS seq
FROM mytable
CROSS JOIN (SELECT @seq :=1, @m :='') AS vars
ORDER BY email) AS t
ORDER BY t.seq, email
输出:
email
-----------------
karthi@gmail.com
ravi@gmail.com
karthi@gmail.com
ravi@gmail.com
karthi@gmail.com
ravi@gmail.com