MySQL Group by two columns from one table
I am struggling with a MySQL select where I am trying to GROUP BY two different columns within a single table. Basically, from my example below you shouldd be able to see that my ID is my key column, and the ThreadID relates to the key column.
----------------------------------
| ID | ThreadID | Heading |
----------------------------------
| 1 | 0 | First heading |
| 2 | 0 | Second heading |
| 3 | 2 | | <--(No heading needed as it relates to ID 2)
| 4 | 2 | | <--(No heading needed as it relates to ID 2)
| 5 | 0 | Third heading |
| 6 | 5 | | <--(No heading needed as it relates to ID 5)
| 7 | 5 | | <--(No heading needed as it relates to ID 5)
----------------------------------
I can get this working great by grouping by, but the problem is that I am getting the following results:
----------------------------------
| ID | ThreadID | Heading |
----------------------------------
| 1 | 0 | First heading |
| 4 | 2 | | <--(Heading of ID 2 disappeared)
| 7 | 5 | | <--(Heading of ID 5 disappeared)
----------------------------------
Basically, if a key column item doesn't have a related record, I am getting the heading printed as desired, if there is a related record, then the heading disappears. My question is, how can I retain the heading?
Here's my recordset select and variable:
SELECT *
FROM tbl_forumPOSTS JOIN tbl_users ON tbl_forumPOSTS.fld_fUSERID = tbl_users.UserID
WHERE fld_fSHOW = 1
GROUP BY fld_fTHREADID, colname
ORDER BY fld_fREPLYDATE DESC
Name: colname
Type: INT
Default: -1
Runtime: $_GET['fld_fID']
With the above recordset and variable, I am able to get the results within my DB table from 99 records down to 16, but I am losing my headings. It's because there isn't one in the record, so I need to tie it to the key record so I can pull it into place, but how?
Thanks in advance.
