使用查询设置MySql Disorder结果

时间:2014-07-11 16:36:06

标签: mysql sql database

晚上好, 我有这样的查询:

SELECT @c:=@c+1 as Count, CurrentISP 
FROM (
  SELECT 'yahoo.com' as currentISP
    UNION ALL SELECT 'yahoo.com'
    UNION ALL SELECT 'gmail.com'
    UNION ALL SELECT 'gmail.com'
    UNION ALL SELECT 'hotmail.com'
    UNION ALL SELECT 'hotmail.com'
  ) t
INNER JOIN ( SELECT @c:=0 )  c

产生如下结果集:

Count  CurrentISP
--
1   yahoo.com
2   yahoo.com
3   gmail.com
4   gmail.com
5   hotmail.com
6   hotmail.com

我想要做的是给这个订单订购如下:

1) yahoo.com
2) gmail.com
3) hotmail.com
4) maybe some ither Email Provider
5) when All providers are over start over again
6) yahoo.com etc etc

我之所以这样做,是因为要避免同时向某个提供商发送电子邮件发送垃圾邮件......所以我可以将发件人得分声誉提高到100%。

2 个答案:

答案 0 :(得分:1)

这有点hacky,但你可以在每次出现名字时按计数排序..这将循环播放你输入的多个记录。

SELECT @D:=@D+1 AS Count, CurrentISP 
FROM 
    ( SELECT @C:=0, @A:=0, @B:=0, @D:=0 )  temp,
    (
        SELECT * 
        FROM (
            SELECT 'yahoo.com' AS currentISP
            UNION ALL SELECT 'yahoo.com'
            UNION ALL SELECT 'gmail.com'
            UNION ALL SELECT 'gmail.com'
            UNION ALL SELECT 'hotmail.com'
            UNION ALL SELECT 'hotmail.com'
        ) t
        ORDER BY
            CASE 
                WHEN currentISP='yahoo.com' THEN @A := @A + 1 
                WHEN currentISP='gmail.com' THEN @B := @B + 1  
                WHEN currentISP='hotmail.com' THEN @C := @C + 1  
            END DESC
    ) AS t

这是查询返回的内容:IMAGE

答案 1 :(得分:0)

如果您想订购重复值列表,可能会更好

select currentIsp
from 
    (
        select 1 as n 
        union select 2
        -- Add as many unique numbers you need
    ) as a,
    (
         select 'yahoo.com' as currentISP
         union select 'gmail.com'
         union select 'hotmail.com'
         -- More UNION queries with unique ISP names
    ) as t
order by a.n, t.currentIsp

(我认为您不需要Count列,但如果您愿意,可以添加它。