2015-06-29 218 views
0

按字母順序排序時出現問題,但最後我想用[重複]排序的名稱我最初想要的名稱是首先按字母順序排序,然後按重複排序名稱排序。重複的可能超過1SQL按字母順序排序按名稱排序

結果我做:

$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]| 
*----------*-----------------* 
+0

您正在使用哪種RDBMS? – realnumber3012

+0

@ realnumber3012 mysql –

回答

0

一種可能的方式做到這一點(假設只每個條目一個副本)首先使用以下選項從列表中選擇唯一記錄:

SELECT DISTINCT 

然後回來在那裏,你可以訂購他們,不過你可以。圍繞您的查詢的第二次將抓住所有不在第一個查詢中的東西。然後你可以訂購這個結果。

+0

沒有一些人可能會有一個以上的副本 –

+0

好的。該算法仍然有效,但只需要更多的迭代。基本上,您在每次重新迭代之前都從列表中刪除不同的元素。可能不是一個理想的方法。 –

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 
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 

計算ROW_NUMBER()對於MySQL

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 
+0

對於重複,它實際上是數據的名稱,因爲我已經設置它每次用戶輸入相同的場地,它會自動將其設置爲xxxx [重複],所以現在我稱之爲與重複字behid場地名稱 –

+0

@Demon Lee,根據你的要求進行編輯。 –

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 

...這應該是一切你需要的。

+0

對於[重複]我已經將它設置爲名稱,每當我創建一個重複的場地時,它將存儲到數據庫中成爲Mastiara [Duplicate]。 –

+0

@DemonLee:在這種情況下,它變得更簡單。見編輯的答案。但是,我認爲向這樣的名稱添加標記是不好的做法。如果其中一位「主人」被刪除,該怎麼辦?或者你想以不同的方式顯示它們?或'GROUP BY'的名字?儘可能乾淨地存儲數據總是一個好主意,並且在查詢時只應用格式/註釋。 –

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; 

注:我剛纔已經改變了查詢,按您的要求,但沒有檢查輸出,所以請讓我知道的問題,如果不得到您想要的輸出,以便我可以更正是否有任何問題。