Mysql 考虑SQL查询是什么,我可以考虑把这个重举移到一个存储的进程中,但是答案让我找到了解决办法,谢谢!谢谢你的客气话!我希望你能理解一切。。祝你好运事实上,我做到了,下次我要做一些像这样疯狂的分组,我要保存这一点逻辑! +--------+----------

Mysql 考虑SQL查询是什么,我可以考虑把这个重举移到一个存储的进程中,但是答案让我找到了解决办法,谢谢!谢谢你的客气话!我希望你能理解一切。。祝你好运事实上,我做到了,下次我要做一些像这样疯狂的分组,我要保存这一点逻辑! +--------+----------,mysql,sql,group-by,Mysql,Sql,Group By,考虑SQL查询是什么,我可以考虑把这个重举移到一个存储的进程中,但是答案让我找到了解决办法,谢谢!谢谢你的客气话!我希望你能理解一切。。祝你好运事实上,我做到了,下次我要做一些像这样疯狂的分组,我要保存这一点逻辑! +--------+------------+--------+--------+ | Fruit | Vegetables | Colors | Number | +--------+------------+--------+--------+ | Apple | Beans


考虑SQL查询是什么,我可以考虑把这个重举移到一个存储的进程中,但是答案让我找到了解决办法,谢谢!谢谢你的客气话!我希望你能理解一切。。祝你好运事实上,我做到了,下次我要做一些像这样疯狂的分组,我要保存这一点逻辑!
+--------+------------+--------+--------+
| Fruit  | Vegetables | Colors | Number |
+--------+------------+--------+--------+
| Apple  | Beans      | Blue   |     10 |
| Apple  | Beans      | Blue   |     10 |
| Apple  | Beans      | Blue   |     20 |
| Apple  | Beans      | Blue   |     20 |
| Apple  | Beans      | Green  |     20 |
| Apple  | Beans      | Green  |     20 |
| Banana | Brocoli    | Red    |     10 |
| Banana | Brocoli    | Red    |     10 |
| Banana | Brocoli    | Blue   |     10 |
| Banana | Beans      | Blue   |     20 |
| Banana | Beans      | Green  |     20 |
| Banana | Beans      | Green  |     20 |
+--------+------------+--------+--------+
SET @a:=0;

select *, @a:=@a+1 as newid FROM(
select fruit, vegetable, color, number

FROM test

GROUP BY fruit, vegetable, color, number) as info
+-------+--------+------------+--------+--------+
| newid | Fruit  | Vegetables | Colors | Number |
+-------+--------+------------+--------+--------+
|     1 | Apple  | Beans      | Blue   |     10 |
|     2 | Apple  | Beans      | Blue   |     20 |
|     3 | Apple  | Beans      | Green  |     20 |
|     4 | Banana | Brocoli    | Red    |     10 |
|     5 | Banana | Brocoli    | Blue   |     10 |
|     6 | Banana | Beans      | Blue   |     20 |
|     7 | Banana | Beans      | Green  |     20 |
+-------+--------+------------+--------+--------+
+----------+--------+------------+--------+--------+
|    ID    | Fruit  | Vegetables | Colors | Number |
+----------+--------+------------+--------+--------+
| Apple-1  | Apple  | Beans      | Blue   |     10 |
| Apple-1  | Apple  | Beans      | Blue   |     10 |
| Apple-2  | Apple  | Beans      | Blue   |     20 |
| Apple-2  | Apple  | Beans      | Blue   |     20 |
| Apple-3  | Apple  | Beans      | Green  |     20 |
| Apple-3  | Apple  | Beans      | Green  |     20 |
| Banana-1 | Banana | Brocoli    | Red    |     10 |
| Banana-1 | Banana | Brocoli    | Red    |     10 |
| Banana-2 | Banana | Brocoli    | Blue   |     10 |
| Banana-3 | Banana | Beans      | Blue   |     20 |
| Banana-4 | Banana | Beans      | Green  |     20 |
| Banana-4 | Banana | Beans      | Green  |     20 |
+----------+--------+------------+--------+--------+
SELECT *, 
       if(@a = fruit, @b := @b + 1, @b := 1) as counter,
       @a := fruit
FROM (
    select fruit, vegetable, color, number
    FROM test
    GROUP BY fruit, vegetable, color, number
) t
CROSS JOIN (SELECT @a := null, @b := null)temp
SELECT 
    CONCAT(fruit, ' ', counter) as fruit, vegetable, color, number
FROM
(   SELECT 
        *, 
        if(@a = fruit, if(@c = color AND @d = number, @b, @b := @b + 1), @b := 1) as counter,
        @a := fruit, @c := color, @d := number
    FROM test
    CROSS JOIN (SELECT @a := null, @b := null, @c := null, @d := null)temp
)t
SELECT CONCAT(r.fruit,'-',r.i) AS ID
     , r.fruit
     , r.vegetable
     , r.color
     , r.number
  FROM ( SELECT IF( t.fruit <=> @fruit
                , IF( t.vegetable <=> @vegetable 
                  AND t.color     <=> @color
                  AND t.number    <=> @number
                  , @i := @i + 0
                  , @i := @i + 1
                  )
                , @i := 1
                ) AS i
              , @fruit     := t.fruit      AS fruit
              , @vegetable := t.vegetable  AS vegetable
              , @color     := t.color      AS color
              , @number    := t.number     AS number
           FROM ( SELECT @i         := 1
                       , @fruit     := NULL
                       , @vegetable := NULL
                       , @color     := NULL
                       , @number    := NULL
                ) i
          CROSS
           JOIN test t
          ORDER 
             BY t.fruit
              , t.vegetable
              , t.color
              , t.number
       ) r