I hope that this better clarifies by question:
I have a table called products :
CREATE TABLE products
(
id
int(11) NOT NULL AUTO_INCREMENT,
user_id
int(11) NOT NULL,
name
text CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
category
text CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
subject
text CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
date_time
datetime DEFAULT NULL,
PRIMARY KEY (id
)
)
I wish to sort my table by three levels
a> category
b> subject
c> types
Now, types are found in a third table called
product_types
CREATE TABLE product_types
(
product_id
int(11) NOT NULL,
type
varchar(255) DEFAULT NULL
)
My data is displayed in the following table headers which can be ‘clicked’ and send a sort value to my sql query which then sorts by those categories:
Name Category Subject Types Date_Time
When I sort my data, I can sort by name, category and subject easily enough as it is in the product table, however product types allow for multiple categories and I am not able to figure out how to sort it.
eg. This is how I sort by name
SELECT products
.id
, products
.name
, products
.category
, products
.subject
, GROUP_CONCAT(product_types
.type
ORDER BY product_types
.type
) as types
, users
.name
, products
.date_time
FROM products LEFT JOIN product_types ON products.id=product_types.product_id INNER JOIN users
ON products
.user_id
=users
.id
GROUP BY products
.id
ORDER BY products
.name
IS NULL, products
.name
And this is how I sort by category
SELECT products
.id
, products
.name
, products
.category
, products
.subject
, GROUP_CONCAT(product_types
.type
ORDER BY product_types
.type
) as types
, users
.name
, products
.date_time
FROM products LEFT JOIN product_types ON products.id=product_types.product_id INNER JOIN users
ON products
.user_id
=users
.id
GROUP BY products
.id
ORDER BY products
.category
IS NULL, products
.category
However, if I try to sort by types because it is a group_concat I am not sure how to sort it. Using the first value is fine as they are displayed alphabetically.
any help deeply appreciated!
thanks
Karen
