SQL按字母顺序排序

时间:2015-06-29 04:10:58

标签: mysql sorting sql-order-by

我有按字母排序的问题,但我希望最后用[重复]排序的名称我最初想要的是按字母顺序排序,然后是最后重复排序的名称。副本可能超过1个。

我做的结果:

$sql  = 'SELECT DISTINCT venue_id, name FROM venues ';
$sql .= 'ORDER BY CASE ';
$sql .= 'WHEN name NOT LIKE "%[DUPLICATE]" THEN "1" ';
$sql .= 'WHEN name LIKE "%[DUPLICATE]" THEN "100" ';
$sql .= 'END ASC ';


   *----------*------------------*
    |     ID   |  venue_name     |
    *----------*------------------*
    | 1       | Axxxx            |
    | 2       | Assss            |
    | 5       | Assss [duplicate]| // this is actually the whole name of the venue
    | 6       | Bzzzz            |
    | 7       | Bzzzz [duplicate]|
    | 8       | Fzzzz            |
    | 21      | Fzzzz [duplicate]|
    *----------*-----------------*

结果可能是:

*----------*------------------*
|     ID   |  venue_name     |
*----------*------------------*
| 1       | Axxxx            |
| 2       | Assss            |
| 6       | Bzzzz            |
| 8       | Fzzzz            |
| 5       | Assss [duplicate]|
| 7       | Bzzzz [duplicate]|
| 21      | Fzzzz [duplicate]|
*----------*-----------------*

6 个答案:

答案 0 :(得分:1)

希望这可能有所帮助

Set Nocount On

Declare @Table Table
(
     Id             Int
    ,venue_name     Varchar(100)
)

Insert Into @Table(Id,venue_name) Values
 (1,'Axxxx')
,(2,'Assss')
,(5,'Assss')
,(6,'Bzzzz')
,(7,'Bzzzz')
,(8,'Fzzzz')
,(21,'Fzzzz')

;With t1 As
(
    Select   t.Id
            ,t.venue_name
    From    @Table As t
            Join
            (
                Select  Min(t.Id) As Id
                From    @Table As t
                Group By t.venue_name
            ) As t1 On t.Id = t1.Id

    Union All

    Select   t.Id
            ,t.venue_name
    From    @Table As t
            Join 
            (
                Select   Min(t.Id) As Id
                        ,t.venue_name
                From    @Table As t
                Group By t.venue_name
            ) As t2 On t.venue_name = t2.venue_name And t.Id > t2.Id
)

Select  *
From    t1

根据您的要求进行修改: -

Insert Into @Table(Id,venue_name) Values
 (1,'Axxxx')
,(2,'Assss')
,(5,'Assss [duplicate]')
,(6,'Bzzzz')
,(7,'Bzzzz [duplicate]')
,(8,'Fzzzz')
,(21,'Fzzzz [duplicate]')

;With t1 As
(
    Select   t.Id
            ,t.venue_name
    From    @Table As t
            Join
            (
                Select  Min(t.Id) As Id
                From    @Table As t
                Group By Replace(t.venue_name,' [duplicate]','')
            ) As t1 On t.Id = t1.Id

    Union All

    Select   t.Id
            ,t.venue_name
    From    @Table As t
            Join 
            (
                Select   Min(t.Id) As Id
                        ,Replace(t.venue_name,' [duplicate]','') As venue_name
                From    @Table As t
                Group By Replace(t.venue_name,' [duplicate]','')
            ) As t2 On Replace(t.venue_name,' [duplicate]','') = t2.venue_name And t.Id > t2.Id
)

Select  *
From    t1

答案 1 :(得分:0)

一种可行的方法(假设每个条目只有一个重复)是首先使用以下列表从列表中选择唯一记录:

ArrayList
然后从那里,您可以按照自己的喜好订购它们。第二次查询将抓住第一个查询中没有的剩余内容。然后你也可以订购这个结果。

答案 2 :(得分:0)

尝试此查询

WITH t1 (row,id,venue_name)
AS
-- Define the CTE query.
(
    SELECT ROW_NUMBER() OVER(PARTITION BY venue_name ORDER BY id ) AS Row
       ,id
      , venue_name
    FROM tablename
    order by venue_name

)
-- Define the outer query referencing the CTE name.
SELECT id,venue_name
FROM t1
order by row

答案 3 :(得分:0)

SELECT 
id, venue_name , 
(SELECT COUNT(*) FROM t t1 WHERE t.venue_name = t1.venue_name ) - 
(SELECT COUNT(*) FROM t t2 WHERE t.id < t2.id AND t.venue_name = venue_name )  AS num
FROM t
ORDER BY num, name

为mySQL计算了row_number()

答案 4 :(得分:0)

试试这个:

select t1.id, t1.name from source_table as t1
    left join (select min(id) as id,
                      name
                 from source_table
             group by name) as t2 on t1.id = t2.id
order by 
    case when t2.name is not null then 0 else 1 end,
    name

SQL fiddle

编辑:使用您的新信息(评论),它变得更简单:

SELECT id, name FROM source_table
ORDER BY
    CASE WHEN name LIKE '%[duplicate]%' THEN 1 ELSE 0 END,
    name

......这应该就是你所需要的一切。

答案 5 :(得分:0)

您也可以查看它,如果它适合您 -

SELECT s.*
FROM (SELECT s.*,
             (@rn := IF(@v = venue_name, @rn + 1,
                        IF(@v := venue_name, 1, 1)
                       )
             ) AS seqnum
      FROM (SELECT venue_name,id FROM venues) s CROSS JOIN
           (SELECT @v := '', @rn := 0) params
      ORDER BY venue_name
     ) s
ORDER BY (seqnum = 1) DESC,
         id ASC;

注意:我刚刚根据您的要求更改了查询,但没有检查输出,所以如果没有得到您想要的输出,请检查并告诉我问题,以便我可以纠正是否有任何问题。