2009-09-10 120 views
3

我有以下行的表:SQL複雜的組查詢

id. type - link 

1. image - http://1 
2. image - http://2 
3. text - none 
4. video - http://.. 
5. image - http://.. 
6. text - http://.. 

我想組類型(圖像)的日期,以便它們顯示爲單行。在這個例子中,前兩個圖像將合併在一起並輸出將是像以下:

1. image - http://1, http://2 ** GROUPED BY DATE, if they are same type and not break type after it. 
2. text - none 
3. video - http://.. 
4. image - http://.. 
5. text - http://.. 

回答

1
SELECT grouper, type, GROUP_CONCAT(link) 
FROM (
     SELECT @group := @group + (NOT (COALESCE(@type, type) = type)) AS grouper, 
       @type := type, 
       m.* 
     FROM (
       SELECT @group := 0, 
         @type := NULL 
       ) vars, 
       mytable m 
     ) q 
GROUP BY 
     grouper 
+0

偉大的作品,但你也可以告訴我,我怎麼只分組圖像,避免文本分組呢? – Basit 2009-09-11 03:27:59

+0

解決方案: SELECT @ groupe,@type,type,GROUP_CONCAT(thumbnail),count(*),date_format(created_date,'%H%k%I%r%T%S%w') FROM( SELECT @group := @group +(NOT(COALESCE(@type,type)= type)or type ='feed')AS grouper, @type:= type, m。* FROM( SELECT @group:= 0, @type:= NULL )瓦爾, 飼料米 爲了通過CREATED_DATE降序 )● GROUP BY石斑魚 – Basit 2009-09-11 05:51:14

+0

這個查詢是工作正常,但直到我開始顯示其他用戶的媒體進料還,這意味着它也有組媒體作爲同一個用戶..我們如何分離即使下一行與前一行相同,也可以分別爲每個不同的用戶分組。 – Basit 2010-01-25 14:24:39

0
select id, type, group_concat(link) from table 

嘗試。

+0

如果你使用MySQL ... – Rabid 2009-09-10 12:10:32

+0

事實上這並不會工作,group by子句缺少 – MaxiWheat 2009-09-10 12:39:47

+0

MaxiWheat:是啊哎呀。 Rabid:這個問題被標記爲mysql – airportyh 2009-09-10 14:15:55

0

下面是一個完整的頭腦彎曲相當於例如用於SQL Server 2005及以後:


create table #Temp 
    (GroupField int, ValueType varchar(10), Value varchar(2048)) 

insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://1') 
insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://2') 
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'text', 'none') 
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'video', '30mins') 
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'image', 'http://5') 
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://4') 
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'text', 'hello') 
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://7') 
insert into #Temp (GroupField, ValueType, Value) VALUES (4, 'image', 'http://0') 

    SELECT GroupField, ValueType, 
      LEFT([Values],LEN([Values]) - 1) AS [Values] 
    FROM (SELECT GroupField, ValueType, 
        (SELECT value + ', ' AS [text()] 
        FROM #Temp AS internal 
        WHERE internal.GroupField = GroupFields.GroupField and internal.ValueType = GroupFields.ValueType 
        FOR xml PATH ('') 
        ) AS [Values] 
      FROM (SELECT GroupField, ValueType 
        FROM  #Temp 
        GROUP BY GroupField, ValueType) AS GroupFields) AS pre_trimmed; 

產生的結果爲:


GroupField ValueType Values 
1   image  http://1, http://2 
2   image  http://5 
2   text   none 
2   video  30mins 
3   image  http://4, http://7 
3   text   hello 
4   image  http://0 

這可能需要一些ORDER BY條款對你的具體情況?改編自Rational Relational - Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005

1

用MySQL你可以這樣做:

SELECT id, type, group_concat(link) 
FROM table 
GROUP BY id, type