I have problem with join left. It work fine but i don't see null results. It connects three tables - categories, product_categories and order_products.
Query made by stack user look's like:
SELECT
categories.name,categories.id,
SUM((orders_products.product_price_gross + orders_products.option_price)*(1 - (orders_products.rebate/100)) * orders_products.product_qty) as suma
FROM orders_products
LEFT JOIN product_categories
ON product_categories.product_id = orders_products.product_id
LEFT JOIN categories
ON product_categories.category_id = categories.id
GROUP BY categories.name, categories.id
ORDER BY suma DESC
I'm not sure how upgrade this query - I need to see what never been sold to show on shop statistics.
I hope You could help me.
Kind regards
Mark
You want to see products that has not been sold? Your main table is the transaction table orders_products. So any product not "ordered" will not show. Interchange product_categories and orders_products to achieve what you want. This will list all categories and null out the "suma" if JOIN statement cannot find the product_id listed in the "order_products" table
SELECT
categories.name,
categories.id,
SUM((orders_products.product_price_gross + orders_products.option_price)*(1 - (orders_products.rebate/100)) * orders_products.product_qty) AS suma
FROM product_categories
LEFT JOIN orders_products
ON product_categories.product_id = orders_products.product_id
LEFT JOIN categories
ON product_categories.category_id = categories.id
GROUP BY categories.name, categories.id
ORDER BY suma DESC
Related
for this i have 3 tables
categories(id,category)
products(id,category_id,name)
productstock(id,product_id,quantity)
the query written for this
select categories.category,sum(productstock.quantity)
from categories
inner join productStock on products.id=productstock.product_id
inner join products on categories.id=products.category_id
group by category;
it would be a great help
Try using LEFT JOIN.
SELECT c.category, SUM(quantity) AS 'sum_qty' FROM productstock a
LEFT JOIN products b ON a.product_id=b.id
LEFT JOIN categories c ON b.category_id=c.id
GROUP BY category_id;
I searched lots of similar topics about this but can't find the answer to this specific probem.
So I have a table with categories and I have another table with products, so what I want is select all the categories that contain at least 1 product, seems very easy but the following code dont give me what I expect.
SELECT *
FROM categories
INNER JOIN products on (categories.id = products.cat_id)
HAVING count(products.cat_id) > 0
All help is appreciated.
Thank you!
You can actually get what you want with just a join because any categories without products will not match.
You should also add a DISTINCT or GROUP BY to remove the duplicate category records from the results:
SELECT DISTINCT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
OR:
SELECT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
GROUP BY c.id
If you want something fancy like categories that have 2 or more products then you can use GROUP BY and HAVING:
SELECT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
GROUP BY c.id
HAVING count(*) >= 2
Try this select in this select all categories that have at least one product
select categories.* from categories
left join products on (categories.id = products.cat_id)
where products.cat_id IS NOT NULL
group by categories.id
In this case you're not using "HAVING" but will have the same result
I've got 3 tables: Product, Shares, Likes which are connected by ProductID. What I want to do is to select all products and COUNT(shares) and COUNT(likes) of these products in one query.
First of all is it possible to do with just one query? If possible how can i do it? And most importantly should I select all products and display then when users hover on make an Ajax call and get like and share data? Thanks in advance.
Here you go:
SELECT
p.id AS 'product id',
IFNULL(COUNT(DISTINCT s.id), 0) AS 'total shares',
IFNULL(COUNT(DISTINCT l.id), 0) AS 'total likes'
FROM
products p
LEFT JOIN shares s ON s.product_id = p.id
LEFT JOIN likes l ON l.product_id = p.id
GROUP BY p.id
IFNULL will treat cases when there are no shares or likes; DISTINCT should be there because otherwise one share will be counted multiple times when joined with likes (and vice versa).
Its possible to do it on one query. Off the top of my head something like
SELECT
product_id,
COUNT(likes.product_id) as likes,
COUNT(SELECT share_id FROM shares WHERE product_id = p.product_id) as shares
FROM product p
LEFT JOIN likes USING(product_id)
Do watch performance though. Make sure you have your indexes etc
SELECT
p.ProductID,
COUNT(s.ProductID) AS SharesCount,
COUNT(l.ProductID) AS LikesCount
FROM
Products p LEFT JOIN Shares s
ON P.ProductID = s.ProductID
LEFT JOIN likes l
ON P.ProductID = l.ProductID
GROUP BY p.ProductID
I have 3 tables
products categories products_categories
------------ ------------- --------------------
product_id category_id product_id
product_title category_title category_id
Each product can belong to multiple categories as the db schema shows.
I join the three tables to get a listing of the three last inserted products.
SELECT p.product_id,
p.product_title,
c.category_id,
c.category_title
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id GROUP BY p.product_id LIMIT 3
With the above query I get the products but with the first category that the product belongs to.
My question is, is it possible by modifying the query to get all the categories that each product belongs to, in one row along with the product info?
Or the only way is to execute another query for each product while fetching the list to get its categories?
You could try:
SELECT p.product_id, p.product_title,
GROUP_CONCAT(c.category_title)
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id
GROUP BY p.product_id
See GROUP_CONCAT syntax here
Dont use "Group By". Then in your PHP Code group manually to product_id and collect all categories.
Given the following tables I would like to know how to write a query to return only the categories that have books in them and the number of books for each category. A book can be added in one or many categories. I'm using PHP and MySQL.
Here are my tables:
categories table
- id
- name
- permalink
books table
- id
- title
- author
- description
- price
books_categories table
- id
- book_id
- category_id
select c.id
,c.name
,count(*) as num_books
from categories c
join books_categories bc on(bc.category_id = c.id)
group
by c.id
,c.name;
Use LEFT JOIN if you also want the categories without books.
SELECT *, COUNT(*) AS count
FROM books_categories AS bc
LEFT JOIN categories AS c ON c.id = bc.category_id
GROUP BY c.id
You'll get a count column with the number of rows for each category. Categories with zero books (that is those with no entry in table books_categories) won't be returned.
SELECT categories.id,
COUNT(books_categories.id) AS number
FROM categories
LEFT JOIN books_categories ON books_categories.category_id = categories.id
GROUP BY categories.id
HAVING number > 0;