I have the following mySQL code:
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
I want to be able to include another COUNT column. But this must only be counted as as the products with p.state = "active". Here's my INCORRECT solution
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory,
COUNT(q.productid) as totalActiveProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
Any help? I have no idea where to go from here...
You can use SUM (CASE WHEN p.state = "active" THEN 1 ELSE 0 END) to get the count you're looking for.
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory,
SUM (CASE WHEN p.state = "active" THEN 1 ELSE 0 END) totalActiveProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
Try this:
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory,
SUM(IF(p.state='active',1,0)) as totalActiveProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
This is similar to #Conrad's but a little cleaner in my opinion
This works but maybe doesn't align with the left join approach so much:
SELECT
c.catId,c.catName,
COUNT(p.productId) as totalProdsInCategory,
(SELECT count(*) from product as p where p.catId=1 AND p.state='active') as totalActive
FROM cat as c
LEFT JOIN product as p ON c.catId = p.catId
WHERE c.catId = 1
GROUP BY c.catId
ORDER BY c.catName ASC
Related
I'm using mysql 8.0.31 version.
Below is my database schema ERD.
I want to get inforamtion about products from my DB.
Below is my sql raw query.
SELECT
p.id, p.name, sc.name sub_category, c.name category, count(po.color_id) color_count
FROM products p
INNER JOIN sub_categories sc
ON sc.id = p.sub_category_id
INNER JOIN categories c
ON sc.category_id = c.id
LEFT JOIN products_options po
ON po.product_id = p.id
INNER JOIN genders g
ON po.gender_id = g.id
WHERE c.id = 1
GROUP BY p.id
ORDER BY p.id ASC;
I want to join products table with sub_categories, categories to get categories information about my products AND products table with products_options, colors, genders to get color_count about a product and gender type about a product.
So I wanted to query like this added g.type (gender type information for a product)
SELECT
p.id, p.name, sc.name sub_category, c.name category, count(po.color_id) color_count, g.type
FROM products p
INNER JOIN sub_categories sc
ON sc.id = p.sub_category_id
INNER JOIN categories c
ON sc.category_id = c.id
LEFT JOIN products_options po
ON po.product_id = p.id
INNER JOIN genders g
ON po.gender_id = g.id
WHERE c.id = 1
GROUP BY p.id
ORDER BY p.id ASC;
But this occurs error like this
SQL Error [1055] [42000]: Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mydb.g.type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
I think this error occurs cuz I didn't use aggregate function for g.type.
Below is my result except g.type in sql select field.
I want to get genders.type field together.
I think I need to sub query and join that table created by sub query. But I don' t know well How can I implement that method.
To combine this gender type information, How can I modify my sql query?
I tried multiple method sql queries in various ways to join table.
You need to either remove the column from select list like:
SELECT p.id, count(po.color_id) color_count
FROM products p
INNER JOIN sub_categories sc
ON sc.id = p.sub_category_i`enter code here`d
INNER JOIN categories c
ON sc.category_id = c.id
LEFT JOIN products_options po
ON po.product_id = p.id
INNER JOIN genders g
ON po.gender_id = g.id
WHERE c.id = 1
GROUP BY p.id
ORDER BY p.id ASC;
or add in group by clause the select columns like:
SELECT p.id, p.name, sc.name sub_category, c.name category, count(po.color_id) color_count, g.type
FROM products p
INNER JOIN sub_categories sc
ON sc.id = p.sub_category_id
INNER JOIN categories c
ON sc.category_id = c.id
LEFT JOIN products_options po
ON po.product_id = p.id
INNER JOIN genders g
ON po.gender_id = g.id
WHERE c.id = 1
GROUP BY p.id,p.name, sc.name sub_category, c.name category, g.type
ORDER BY p.id ASC;
Basically, I have a coppermine gallery and I want to show the last 4 updated albums on the homepage. Here's the query that I've got so far. It basically gets the latest pictures. The subquery works fine on it's own but when it comes time to grouping them to get each album on its own, it doesn't seem to be getting the most recent one from the list.
SELECT *
FROM (
SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
WHERE p.approved='YES' AND a.category IN (47,48)
ORDER BY p.ctime DESC) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4
I figured out the answer. Apparently, in MariaDB you have to give the subquery a limit for it to be sorted correctly. So:
SELECT *
FROM (
SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
WHERE p.approved='YES' AND a.category IN (47,48)
ORDER BY p.ctime DESC
LIMIT 200) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4
I have the following query:
SELECT c.`entity_id`,c. p.`name` as parent_id, c.`name`
FROM category c
LEFT JOIN category p WHERE c.`parent_id = p.id
WHERE c.is_active = 1
ORDER BY c.parent_id ASC, c.position ASC;
But I get an error on the first WHERE clause, can anyone spot the error here? Thanks
You have added where clause twice. Try as below :
SELECT c.`entity_id`,c. p.`name` as parent_id, c.`name`
FROM category c
LEFT JOIN category p on c.`parent_id = p.id
Where c.is_active = 1
ORDER BY c.parent_id ASC, c.position ASC;
I want to get all the detail about my purchase from the following tables,
1) tbl_track
2) tbl_album
3) tbl_purchase
tbl _purchase table :-
item_id
type
price
tbl_track table :-
id
title
description
tbl_album table :-
id
title
description
Now based on type of tbl_purchase I want to join one of table,
It means If type = 0 then LEFT JOIN with tbl_track and for type = 1 then LEFT JOIN with tbl_album.
I want to do these with one query only, It is easily done with two query.
Thanks in advance.
With UNION:
SELECT p.type, t.id, t.title, t.description
FROM tbl_purchase p
JOIN tbl_track t ON t.id = p.item_id AND p.type = 0
UNION
SELECT p.type, a.id, a.title, a.description
FROM tbl_purchase p
JOIN tbl_album a ON a.id = p.item_id AND p.type = 1
I think you need left join both tables in any case. So you can use next query for example
SELECT P.*,
T.id AS track_id,
A.id AS album_id
FROM tbl_purchase AS P
LEFT JOIN tbl_track AS T ON P.item_id = T.id
AND P.type = 0
LEFT JOIN tbl_album AS A ON P.item_id = A.item_id
AND P.type = 1
Try this:
SELECT *
FROM tbl_purchase A
LEFT OUTER JOIN tbl_track B ON A.item_id = B.id AND A.type = 0
LEFT OUTER JOIN tbl_album C ON A.item_id = C.id AND A.type = 1;
I have three tables:
products (product_id, title)
comments (comment_id, product_id, user_id, comment, post_date)
bookmarks (user_id, product_id, read_date)
For each product_id in the products table, I wish to retrieve the number of comments with the same product_id, and whose post_date value is greater than the read_date value for the row in the bookmarks table that shares this product_id, and has user_id=22.
If such a row does not exist in the bookmarks table, I want to retrieve the total number of comments for that product_id regardless of read_date.
So far I have
SELECT p.product_id, COUNT( c.comment_id ) comment_count
FROM products p
LEFT JOIN bookmarks b, comments c ON b.product_id = c.product_id
AND b.user_id =22
AND (
c.post_date > b.read_date
)
AND p.product_id = c.product_id
GROUP BY c.product_id
ORDER BY comment_count DESC
This does not give me the expected results. How can I modify it to make it do what I want?
Will it work for you ?
SELECT p.product_id,
COUNT(CASE
WHEN b.read_date IS NOT NULL AND c.post_date >b.read_date THEN c.comment_id
WHEN b.read_date IS NULL THEN c.comment_id
ELSE NULL //optional, CASE has default ELSE NULL
END) as comment_count
FROM products p
LEFT JOIN bookmarks b ON (b.product_id = p.product_id AND b.user_id=22)
LEFT JOIN comments c ON (p.product_id = c.product_id)
GROUP BY p.product_id
ORDER BY comment_count DESC
UPDATE
GROUP BY c.product_id changed to GROUP BY p.product_id
Maybe this will work for you or atleast point you in the right direction.
SELECT p.product_id COUNT( c.comment_id ) comment_count
FROM products p
LEFT JOIN comments c on c.product_id = p.product_id
LEFT JOIN bookmarks b on b.product_id = c.product_id
WHERE (p.product_id IN (
SELECT *
FROM bookmarks b
WHERE b.user_id = 22
)
AND
c.post_date > b.read_date
)
OR
p.product_id NOT IN (
SELECT *
FROM bookmarks b
WHERE b.user_id = 22
)
GROUP BY c.product_id
ORDER BY comment_count DESC