My sql query to get middle row values using GROUP BY function

后端 未结 2 1607
时光说笑
时光说笑 2021-01-13 11:02

My database structure is like

Id     Price     Code
1      0.12      93
2      0.13      93
3      0.54      93
4      0.96      93
5      0.10      94
6             


        
相关标签:
2条回答
  • 2021-01-13 11:06

    It can be done by some tricks.

    SELECT id, 
           price, 
           code 
    FROM   table1 
    WHERE  id IN (SELECT Ceil(Avg(id)) AS `id` 
                  FROM   table1 
                  GROUP  BY code); 
    

    SQLFiddle

    0 讨论(0)
  • 2021-01-13 11:13
    SELECT table1.* 
    FROM table1
    JOIN (
        SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( GROUP_CONCAT(id ORDER BY id ASC), ',', CEIL(COUNT(*) / 2) ), ',', -1) AS id
        FROM table1
        GROUP BY CODE
    ) t USING(id) 
    

    http://sqlfiddle.com/#!2/fdc22/14

    0 讨论(0)
提交回复
热议问题