I have the following tables:
products table
-------------
id
name
...
categories table
----------------
id
name
product_categories table
------------------------
product_id
category_id
product_ratings
---------------
product_id
user_id
rating (1 to 5 INT)
How can I select (search) products and rating (average) by Category (name or id) and order them by Title or by rating.
I have tried some queries but im having some trouble on how to join tables and how to use where clause
Do you mean something like that?
select
products.name, categories.name, product_ratings.rating
from products
join product_categories on products.id = product_categories.product_id
join categories on categories.id = product_categories.category_id
join product_ratings on product_ratings.product_id = products.id
where
categories.name = 'CATEGORY NAME'
order by
product_ratings.rating
select p.name, r.rating
from products as p
inner join product_ratings as r on p.id = r.product_id
inner join categories as c on c.id = pc.category_id
inner join product_categories as pc = pc.product_id = p.id
where c.name = 'search_name'
order by r.rating;
Method 1
Rating by product and user id
SELECT
P.name, PR.user_id , IFNULL(PR.rating,0) as rating
FROM products P
JOIN product_categories PC ON P.id = PC.product_id
JOIN categories C ON C.id = PC.category_id
JOIN product_ratings PR ON PR.product_id = P.id
WHERE C.name = 'Category A'
ORDER BY P.name
Refer http://sqlfiddle.com/#!9/014af/6
Method 2
Rating by product id
Different users will rate the same product, so we have to consider the average rating of each product.
SELECT
P.name, GROUP_CONCAT(PR.user_id) as user_ids, AVG(IFNULL(PR.rating,0)) as rating
FROM products P
JOIN product_categories PC ON P.id = PC.product_id
JOIN categories C ON C.id = PC.category_id
JOIN product_ratings PR ON PR.product_id = P.id
WHERE C.name = 'Category A'
GROUP BY P.id
ORDER BY P.name
Refer http://sqlfiddle.com/#!9/014af/7
Related
This query gives an error if subquery return more than 1 row. I separated the queries and use mysqli_multi_query(), but both queries data is displayed in two tables.
So I decided to make the one query.
SELECT DISTINCT category ,
(SELECT COUNT(products.name)
FROM products
where category_id=categories.id
) AS total_products,
(
SELECT SUM(quantity) FROM productstock a
LEFT JOIN products b ON a.product_id=b.id
LEFT JOIN categories c ON b.category_id=c.id
where c.deleted=0
GROUP BY category_id
) AS available_stock,
SUM(product_qty*orignalCost) AS SaleWise_cost,
SUM(product_qty*saleprice) AS SaleWise_price,
SUM(product_qty*saleprice) AS total_sale ,
SUM((product_qty*saleprice)-(product_qty*orignalCost)) AS profit
FROM categories
INNER JOIN products ON categories.id = products.category_id
INNER JOIN sales ON sales.product_id = products.id
INNER JOIN productstock ON productstock.product_id = products.id
WHERE categories.deleted=0
GROUP BY category_id
As available stock is corelated subquery so joining condition must be added in where clause inside subquery. please check this pseudocode
(
SELECT SUM(quantity) FROM productstock a
LEFT JOIN products b ON a.product_id=b.id
LEFT JOIN categories c ON b.category_id=c.id
where c.deleted=0 AND b.category_id = categories.id
) AS available_stock
Another way
(SELECT SUM(quantity)
FROM products b
INNER JOIN productstock a
ON b.id = a.product_id
AND b.id = products.id
AND b.category_id = categories.id) AS available_stock
I have 3 tables products , category and product_category here are strutures of tables
products = id(pk), name , sku, ...
category = id(pk),name,parent_id(self relation)
product_category = category_id(fk), product_id(fk)
here is my query so for running
SELECT `products`.*,`product_category`.`category_id` as catid FROM `products` LEFT JOIN `product_category` ON products.id = product_category.item_id GROUP BY `products`.`id`
it brings all products and category_id from product_category table i want data from category table as well like category.name . how i can do this?
Is this not as simple as..?
SELECT *
FROM products p
JOIN product_category pc ON p.product_id = pc.product_ID
JOIN category c ON pc.category_id = c.category_id;
Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");
I have a simple database
With this query I get all users and number of products they have bought:
SELECT
c.id,
cd.name,
cd.surname,
COUNT(p.name) as bought
FROM shopping s
JOIN client c ON s.client_id = c.id
JOIN client_details cd ON c.id = cd.client_id
JOIN product p ON s.product_id = p.id
GROUP BY c.id;
It's pretty good, but I have to select all users and number of specific products bought. With this query I get it only if the user have bought at least one product:
SELECT
c.id,
cd.name,
cd.surname,
COUNT(p.name) as bought
FROM shopping s
JOIN client c ON s.client_id = c.id
JOIN client_details cd ON c.id = cd.client_id
JOIN product p ON s.product_id = p.id
WHERE p.name = 'sugar'
GROUP BY c.id;
I want to get the list with all users, and if specific user didn' buy specific product, there should be 0 on output.
Use condition aggregation:
SELECT c.id, cd.name, cd.surname,
MAX(p.name = 'sugar') as SugarFlag
FROM shopping s JOIN
client c
ON s.client_id = c.id JOIN
client_details cd
ON c.id = cd.client_id JOIN
product p
ON s.product_id = p.id
GROUP BY c.id;
I have got 2 tables: categories and products.
Categories have parent-child relationship structure and the data is fetched by joining the same table.
When fetching the data, I have to count:
how many products each category contains which have stock >= 1
how many subcategories a category contains which contain at least 1 product with stock >= 1
SELECT c. * , count( DISTINCT s.cat_id ) AS number_of_subcategories, count( DISTINCT p.id ) AS number_of_products FROM categories c
LEFT JOIN categories s
ON s.parent_id = c.cat_id
LEFT JOIN products p
ON p.cat_id = c.cat_id AND p.stock >= 1
GROUP BY c.cat_name
ORDER BY number_of_products ASC
At the first glance all goes well, but unfortunately I get total number of all subcategories.
Do I miss one more join or what is my problem so far?
Here is the code: SQLFiddle
You could alter your query to use a subquery to get the number of subcategories similar to this:
SELECT c.cat_id,
c.parent_id,
c.cat_name,
count(sp.cat_id) AS number_of_subcategories,
count(DISTINCT p.id) AS number_of_products
FROM `categories` c
LEFT JOIN
(
select distinct s.cat_id, s.parent_id
from categories s
inner join products p
on s.cat_id = p.cat_id
where p.stock > 1
) sp
ON sp.parent_id = c.cat_id
LEFT JOIN products p
ON p.cat_id = c.cat_id
AND p.stock >= 1
GROUP BY c.cat_id, c.parent_id, c.cat_name;
See SQL Fiddle with Demo
Try changing AND for WHERE. Does it work?
Francisco