简体   繁体   中英

Display each row one by one from each category one by one using php mysql

i have the following table structure: basically it is grouping by agency_id.

 id    agency_id    price         type
 1      1001           10000       A 
 2      1002           13000       B
 3      1001           16000       C
 4      1003           11000       A
 5      1002           12000       C
 6      1003           9000        D
 7      1001           15000       A
 8      1002           12000       A  

i want to display it as following

 id    agency_id     price        
 1      1001         10000        
 2      1002         13000        
 4      1003         11000        

 3      1001         16000        
 5      1002         12000   
 6      1003         11000

i am using the following code, but its showing all the properties from 1 agency, then 2nd agency and so on.

ORDER BY
  CASE properties.agency_id
      WHEN 1001 THEN 1
      WHEN 1002 THEN 2
      WHEN 1003 THEN 3
  END

but its not giving me the desire results. any help will be apriciated

Regards,

Use a solution like the one in Ranking by Group in MySQL to add a rank column that increments within each group, and put that in a subquery. Then do:

SELECT *
FROM (subquery) AS x
ORDER by rank, agency_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM