PHP & MYSQL: using group by for categories

前端 未结 6 709
栀梦
栀梦 2020-12-08 08:54

My database has the following setup

productid | productname | category id

and I want to output them like so:

category #1
it         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 09:50

    What you want is to order them by the category, not group them.

    SELECT * FROM MyTable
    ORDER BY category_id, product_id
    

    When you iterate through the list, just output a new header whenever the category_id changes.

提交回复
热议问题