php - Basic SQL query design issue -
i'm trying create query equivelant of (does not work).
select * `categories` c , select * `items` , select count(i.id) items , select sum(i.price) price;
i not using sqlserver, , i'm using pdo through php database connectivity.
here's tables.
category
+----+------------+ | id | category | +----+------------+ | 1 | first_cat | | 2 | second_cat | +----+------------+
items
+----+----------+------+-------+ | id | category | name | price | +----+----------+------+-------+ | 1 | 1 | foo | 1.99 | | 2 | 1 | bar | 2.00 | | 3 | 2 | oof | 0.99 | | 4 | 2 | rab | 1.99 | +----+----------+------+-------+
based on these tables expecting these query results:
+----+------------+-------+-------+--+ | id | category | items | price | | +----+------------+-------+-------+--+ | 1 | first_cat | 2 | 3.99 | | | 2 | second_cat | 2 | 2.98 | | +----+------------+-------+-------+--+
any help?
the query have posted in comment not joining category table , item table. makes me think this?:
select categories.id, categories.category, count(*) items, sum(items.price) price `categories` join items on categories.id = items.category group categories.id, categories.category
Comments
Post a Comment