I have two tables. One is a category ID, the other one is a product table. I would like to count how many products of each category ID, and the query is below.
SELECT hkgg_emall_goods_class.gc_id, COUNT(*) as productcount
FROM hkgg_emall_goods_class
LEFT JOIN hkgg_emall_goods
ON hkgg_emall_goods.gc_id=hkgg_emall_goods_class.gc_id GROUP BY hkgg_emall_goods_class.gc_id ;
It shows what I want, except the query shows some rows to have count of 1 even they have no products associated, and some row as 1 count when they actually have one product associated.
I want your advice on
1) how to solve this problem
2) I have added the gc_productcount column in the category table. How can I insert the count query into the gc_productcount column for every row?
INSERT INTO `hkgg_emall_goods_class.gc_productcount`
This query is not working well when I put it in front of the select count query.
P.S. I have browsed the other thread in stackoverflow, but luck is not good enough to browse a similar solution.
Thank you in advance.
Assuming hkgg_emall_goods table has a primary or at least a unique key, that's what you want to count. i.e. you don't want to COUNT(*), you want to COUNT(hkgg_emall_goods.id).
So assuming that primary key is hkgg_emall_goods.id then your query will look like this:
SELECT
hgc.gc_id,
COUNT(hg.id) AS productcount
FROM hkgg_emall_goods_class hgc
LEFT JOIN hkgg_emall_goods hg ON hg.gc_id = hgc.gc_id
GROUP BY
hgc.gc_id
Related
I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);
I put together this example to help
http://sqlfiddle.com/#!9/51db24
The idea is I have 3 tables. One is the "root" table, in this case person which has a score attached to it. They then have some category I need to group by person_cat.cat and a one to many field called CS.
I would like to query for the average of the score, the sum of the one to many field person_co.co and group by the category.
SELECT
person_cat.cat,
person.id,
SUM(person_co.co),
AVG(person.cs)
FROM
person
LEFT JOIN person_co USING (id)
LEFT JOIN person_cat USING (id)
GROUP BY cat;
The issue I'm currently having is the average gets thrown off due to the join for the one to many. I can accomplish this with multiple queries, which is ok if that is the answer. However it would be nice to get this as one query.
I have a website with products where some products are duplicated, and the duplication is only because sometimes the same products goes under more than one categories. I just want the unique columns of the product, not the duplicate (that has another ID and another Category_id). I know the problem could be solved if the table was normalized, but I didn't develop these tables and I can't redesign the database now.
So basically I'm trying to something that logically looks like this (but the code below still gets the repeated products):
SELECT id
FROM `website_products`
WHERE p_name_en
IN (
SELECT DISTINCT p_name_en
FROM `website_products`
)
Do you just want:
select distinct id
from website_products
Or are you trying to get distinct product names with a single id:
select p_name_en, id
from website_products wp
group by p_name_en;
You Can Try like this,,,
SELECT id
FROM `website_products
group by p_name_en
My table is set up like this, all I need to do is call a query to my Photos table.
I have PhotoID as the primary key and GalleryID as the foreign key to Gallery. How can I count the number of unique PhotoID's for each multiple GalleryIDs.
So to speak there are may duplicate GalleryIDs because there are many photos in a gallery. So I just need to could the number of unique PhotoIDs associated with that GalleryID.
Can it be done in one query?
Use this:
SELECT GalleryID, count(PhotoID)
FROM Photos
GROUP BY GalleryID;
The usual way is to use JOIN and GROUP BY clauses like so:
SELECT `Gallery`.`GalleryID`, COUNT(`Photos`.`PhotoID`) AS `NumOfPhotos` FROM `Gallery`
LEFT JOIN `Photos` ON `Gallery`.`GalleryID` = `Photos`.`GalleryID`
GROUP BY `Gallery`.`GalleryID`;
This will count the number of Photos.PhotoID entries grouped by the Gallery Id.
In this case, you could do the query without the JOIN, by just querying the Photos table, but if you want more than just the GalleryID from the gallery table, you need the join.
uses the following sql
select count(photoid) as noOfPhotos from photos group by galleryid;
use where on galleryid to get result for specific gallerie(s)
I have a problem formulating a MySQL query to do the following task, although I have seen similar queries discussed here, they are sufficiently different from this one to snooker my attempts to transpose them. The problem is (fairly) simple to state. I have three tables, 'members', 'dog_shareoffered' and 'dog_sharewanted'. Members may have zero, one or more adverts for things they want to sell or want to buy, and the details are stored in the corresponding offered or wanted table, together with the id of the member who placed the ad. The column 'id' is unique to the member, and common to all three tables. The query I want is to ask how many members have NOT placed an ad in either table.
I have tried several ways of asking this. The closest I can get is a query that doesn't crash! (I am not a MySQL expert by any means). The following I have put together from what I gleaned from other examples, but it returns zero rows, where I know the result should be greater than zero.
SELECT id
FROM members
WHERE id IN (SELECT id
FROM dog_sharewanted
WHERE id IS NULL)
AND id IN (SELECT id
FROM dog_shareoffered
WHERE id IS NULL)
THis query looks pleasingly simple to understand, unlike the 'JOIN's' I've seen but I am guessing that maybe I need some sort of Join, but how would that look in this case?
If you want no ads in either table, then the sort of query you are after is:
SELECT id
FROM members
WHERE id NOT IN ( any id from any other table )
To select ids from other tables:
SELECT id
FROM <othertable>
Hence:
SELECT id
FROM members
WHERE id NOT IN (SELECT id FROM dog_shareoffered)
AND id NOT IN (SELECT id FROM dog_sharewanted)
I added the 'SELECT DISTINCT' because one member may put in many ads, but there's only one id. I used to have a SELECT DISTINCT in the subqueries above but as comments below mention, this is not necessary.
If you wanted to avoid a sub-query (a possible performance increase, depending..) you could use some LEFT JOINs:
SELECT members.id
FROM members
LEFT JOIN dog_shareoffered
ON dog_shareoffered.id = members.id
LEFT JOIN dog_sharewanted
ON dog_sharewanted.id = members.id
WHERE dog_shareoffered.id IS NULL
AND dog_sharewanted.id IS NULL
Why this works:
It takes the table members and joins it to the other two tables on the id column.
The LEFT JOIN means that if a member exists in the members table but not the table we're joining to (e.g. dog_shareoffered), then the corresponding dog_shareoffered columns will have NULL in them.
So, the WHERE condition picks out rows where there's a NULL id in both dog_shareoffered and dog_sharewanted, meaning we've found ids in members with no corresponding id in the other two tables.