Display group's first element as parent and other as children

I have this query:

SELECT * FROM table GROUP BY code, company, status ORDER BY company LIMIT 15

I want to display group’s first element as parent and other as child, like:

Company Name 1
-- Compnay Name 1 | Code | Status
-- Company Name 2 | Code | Status

Company Name 3
--Company Name 3 | Code | Status

Company Name 4
--Company Name 4 | Code | Status
--Company Name 5 | Code | Status

Also there should limit of 15 parents. Please let me how to loop though this to output like this using PHP (DB is PGSQL)?

Can you given your example with a little more (fictional) detail?

E.g. codes and statuses, so that I can be a bit more clear about what you mean.

Logically speaking, and probably not so relevant to your question, I can’t really see what you wish to achieve with this kind of output. Why would two companies be associated together, under the heading of ONE of the companies, by a certain value in the database? If they are related, then surely a more logical database layout would be beneficial.

result are being grouped if they have any one company, code, status similarity. for example


company | code | status
----------------------------------------
CN1        | 02     | A
-----------------------------------------
CN1        |  01    | B
----------------------------------------
CN3        |  01    | C
-------------------------------------
CN4       | 05     | C
-------------------------------------
CN7      |  08     | E
--------------------------------------
CN8     | 09      | E

result should be like:


CN1
--CN1   |   02   | A
--CN1   |  01    | B
--CN3   |  01    | C
--CN4   |  05    | C

CN7
--CN7  | 08   | E
--CN8  | 09   | E 

Please notice a group has any one of three column with similar value. First record in this group should be it parent/title. Thanks!