Hi guy's I've been trying to figure this out for hours, but I can't seem to wrap my head around it..
Take a look at this sql table..
I'm trying to search for a product_id that exists in all filter_cat_id's, so in this example it would be product_id 30, because it exists with filter_cat_id 1 2 and 3.. Product_id 30 exists in the same filter_cat_id twice, otherwise I'd just count the product id's, then the distinct cat id's and make sure the count's match.
Any ideas? Is this possible in mysql? another option would be to use PHP.
Thanks!
I'm trying to search for a product_id that exists in all filter_cat_id's, so in this example it would be product_id 30, because it exists with filter_cat_id 1 2 and 3.
In general, this can be done by grouping by product_id and using HAVING COUNT(product_id) >= x – where x is the number of categories,
SELECT product_id FROM table GROUP BY product_id HAVING COUNT(product_id) >= 3
If you don’t know the number of categories upfront (so that you can insert the actual number into the query), you can get that value with a subquery,
SELECT product_id FROM table GROUP BY product_id
HAVING COUNT(product_id) >= (SELECT COUNT(DISTINCT category_id) FROM table)
Edit: OK, so apparently the category_ids to search for come from some kind of search form/filter mechanism.
In that case, the filtering of items has to be added – and the number to compare to must be embedded implicitly, but that should be no problem.
SELECT product_id FROM table
WHERE category_id IN(1,2,3)
GROUP BY product_id
HAVING COUNT(product_id) >= 3
SELECT
product_id, count(i.product_id) as t
FROM
(SELECT
*
FROM
tablename
GROUP BY product_id , filter_cat_id) as i
GROUP BY product_id
HAVING t = 3
This should be smart enough to determine the number of distinct categories, and select a product_id that has that number of distinct categories.
SELECT product_id
FROM
(SELECT DISTINCT product_id, filter_cat_id
FROM test.play) a
GROUP BY product_id
HAVING count(product_id) = (SELECT count(DISTINCT filter_cat_id) FROM test.play)
;
Test Data:
Query Result:
30
40
Related
I have a table named items which has 3 columns : id, user_id, item_name.
I want to select and show all users that have most submitted items in that table.
For instance :
User-1 has 3 items,
User-2 has 8 items,
User-3 has 5 items,
User-4 has 8 items, and
User-5 has 8 items too.
Based on what I need, the query should be outputting User-2, User-4 and User-5.
My knowledge of MySQL is not thorough unfortunately and I can't get this done by myself.
Your help is much appreciated.
EDIT #1 :
Here's the query that I tried and didn't output my desired result :
SELECT COUNT(id) AS total_count
, user_id
FROM ".DB_PREFIX."items
GROUP
BY user_id
It shows all users and their total number of items submitted. As I mentioned earlier, I need all top users.
E.g.:
SELECT a.*
FROM
( SELECT user_id
, COUNT(*) total
FROM my_table
GROUP
BY user_id
) a
JOIN
( SELECT COUNT(*) total
FROM my_table
GROUP
BY user_id
ORDER
BY total DESC
LIMIT 1
) b
ON b.total = a.total;
I have a table with
id primary key, nominee_id, cat_id, user_id(voter id), vote_status, date
these fields, We have nominees for this voting process under different categories, so each nominees can be nimnated to more than one category. ok, all process is going well, I can take total count of votes got for each nominees in each category. but I dont have any idea How to get the winner from this table using SQL.
I want to get Most votes gained Nominees in each category, however as I said I can get total votes got for each nominees in each category using
SELECT nominee_id FROM voting WHEREcat_id= $cid.
Is it possible get this through another SQL statement, or else can anyone suggest any other way to get this.
below is the table, I want to get back the Nominee_id who got max Vote in a particulat cat_id, eg: in hte below table I want to get nominee_id 29 as a winner in cat_id 3, because he got two votes in that category
This query will give you the winner from each category.
SELECT nid, cid, max(votes) as final_votes from (select nominee_id as nid, cat_id as cid, count(user_id) as votes from voting group by cat_id, nominee_id) nv GROUP BY cid order by final_votes desc;
This one should work
SELECT nominee_id, COUNT(nominee_id) AS countNominee FROM voting WHERE
vote_status = 'yes' GROUP BY cat_id ORDER BY countNominee DESC
I have a shop that sells items
Every time an item is sold, following data is placed in a database:
-item_id
-item_count
-bought_value (money spent to buy this item, taking amount into consideration)
Now I would like to know the percentage of money that has come in PER ITEM
I would like to have this information sorted but I'm not fully sure how to do this
What I currently have is:
to find total money that has come in: SUM(boughtvalue)
to find all items: SELECT DISTINCT item_id
iterate through all items and use: SELECT SUM(boughtvalue) WHERE item_id = ...
This gets me the results, but they're not sorted this way
What I'm looking for is something like "SELECT DISTINCT item_id ORDER BY SUM(boughtvalue)"
My PHP script that finds this information currently displays something like this:
15332-0.75640207175834%
18353-0.30683127676158%
18349-0.53882565675204%
18351-0.20954331095913%
All I need is to have this information sorted.
You are looking for group by:
SELECT item_id, SUM(boughtvalue)
FROM table t
GROUP BY item_id
ORDER BY SUM(boughtvalue) DESC;
Try something like:
select item_id, sum(item_count), sum(bought_value) from item_table group by item_id
assuming your table name is item_table
Play around with something like this
SELECT item_id,
(
SUM( boughtvalue ) / ( SELECT SUM(boughtvalue) from tbl )
) as percentvalue
FROM tbl
GROUP BY item_id
ORDER BY percentvalue DESC;
Ususally I discourage subqueries, but I think the optimizer will optimize that away.
Try:
SELECT item_id, SUM(bought_value) as bought_value FROM item_table GROUP BY item_id ORDER BY bought_value
I'm making a sitemap. I have a table, which connects posts with categories (2 columns: categoryId, postId). There are nearly 9000 categories in total, and I want to count number of posts in each category to predict number of pages in pagination. I tried to do 9000 COUNT(*)'s for each category, but that is too slow (2 hours, actually). What can I do? Thanks.
Use the GROUP BY function
SELECT C, COUNT(*)
FROM TABLE
GROUP BY categoryId as C
You just have to do a SELECT COUNT and GROUP it by your categoryid like this :
SELECT C, COUNT(*) FROM yourtable GROUP BY categoryId as C
If you want only for a specific category, try to use WHERE categoryID = ?
before the GROUP BY
You can count it and group it by CategoryID simple as that
select Count(postid) as NumberOfPosts, categoryid from your_table group by categoryid
and that is simple as that.
Best solution for you is to create table where you will hold real statistic and increase it when post is created or decrease it when post is deleted.
NEW
if you want for certain category then just add to query where categoryid=?
so your query should look like
select Count(postid) as NumberOfPosts from your_table where categoryid=?
or if you want to do search for more than one category than just do this
select Count(postid) as NumberOfPosts, categoryid from your_table where categoryid in (1,2,3,4)
group by categoryid
I have a SQL database of products from different suppliers, so the same product could appear multiple times with different prices.
Is it possible to select all of the products that have more than 4 prices, so bascially all of the rows which have more than 4 rows with the same ID?
You could add COUNT(*) AS number_of_products to SELECT, GROUP BY product_id, then use HAVING number_of_products > 4.
Note that HAVING is applied on the results (it basically goes through all the results, one by one, and applies the conditions), so it will be slower than WHERE. If you have hundreds of thousands of rows and you need performance, consider pre-counting the products, storing the indexed count value somewhere, then using a simple WHERE instead.
Yes, GROUP on the identificator for your item, and specify the number of prices to count in a HAVING clause, something like this :
SELECT ItemID, COUNT(Price) FROM itemTBL GROUP BY ItemID HAVING COUNT(Price) >= 4
You can then use this to later filter and get more information:
SELECT Item.*, Category.Name, Filter.NumPrices from itemTBL AS Item
INNER JOIN categoryTBL as Category ON Item.CategoryID = Category.CategoryID
INNER JOIN (SELECT ItemID, COUNT(Price) AS NumPrices FROM itemTBL GROUP BY ItemID HAVING COUNT(Price) >= 4) AS Filter on Item.ItemID = Filter.ItemID
This is the perfect thing for a group by:
SELECT ProductID, COUNT(*) AS PriceCount
FROM Product GROUP BY
Product,Price HAVING COUNT(Product) > 4