Basically I do while two time to get main group & group value. Example
$group_query = $db->query("
SELECT opt_id, opt_type
FROM ads_options
WHERE opt_id = '" . intval($data['ad_id']) . "'
GROUP BY opt_type");
while ($group_data = $db->fetch($group_query)) {
$option_query = $db->query("
SELECT *
FROM ads_options
WHERE opt_id = '" . intval($data['ad_id']) . "'
AND opt_type ='" . $group_data['opt_type'] . "'
ORDER BY opt_id DESC");
while ($option_data = $db->fetch($option_query)) {
}
}
Output :
Size : S
M
L
Color : White
Black
Question :
How to join current queries above with single statement?
Update :
Current database structure
opt_id opt_type opt_name opt_price
1236 Size S 0
1236 Size M 1
1236 Color Black 1
1236 Color White 2
Something like this would get the unique combinations, may be of help
If you are able to supply more details as to precisely what you are trying to achieve, and the full design of ads_options, then it would be easier to provide more specific advice
Also a minor note, if opt_id is numeric then you don’t need single quotes (‘) around it within the query (so removed them in example)
EDIT
Something like the following would return you a list of id, type, and then as third field a comma-delimited list of values, if that’s of more help
For more information on GROUP_CONCAT check out this link http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
I’ve no idea if PHP programatically has something to help with this, I tend to use Coldfusion so if anyone knows of a PHP version of doing the following then that seems to match what OP was asking for
http://bytes.com/topic/php/answers/11929-grouping-results-query#post50606 seems to suggest the following as potentially a match for the I posted above so may be useful
You then iterate over $answer. This would play nicely with