I have 2 tables.
Table 1: Fruits
id (int, autoincreasing, primary key)
some other junk
Table 2: Customers
has a 'fruit' column, this column contains an id from the fruit table.
i want to query all the customers, and come up with a list of all the fruit ID's and the number of times they are in use.
So this set up:
Fruits has
id name
1 orange
2 banana
3 apple
Customers has 6 rows like:
id fruit
1 1
2 1
3 2
4 2
5 2
6 3
Trying to write a query that Will give me:
fruit id purchase count
1 2
2 4
3 1
Try this
SELECT f.id as fruit_id, COUNT(1) AS purchase_count
FROM FRUITS f LEFT JOIN CUSTOMERS c
ON F.ID = c.fruit
GROUP BY f.id
Should just be a simple aggregate...
SELECT fruit_id, COUNT(fruit_id) AS purchase_count
FROM customers
GROUP BY fruit_id
ORDER BY fruit_id ASC;
untested
SELECT id, SUM(fruit) as "fruit id", "purchase count"
FROM Customers
GROUP BY id;
Related
Suppose I have these MySql tables:
TABLE_PEOPLE
id
name
1
John
2
Albert
3
Joanna
4
Mike
5
Norton
TABLE_COLOR
id
people_id
colors
1
1
Green
2
1
Red
3
3
Yellow
4
3
Blue
5
2
Green
6
4
Red
7
5
Grey
8
3
White
9
4
Black
10
1
Black
TABLE_FRUIT
id
people_id
fruits
1
1
Lemon
2
2
Apple
3
3
Tangerine
4
5
Orange
5
2
Banana
6
1
Apple
7
5
Lemon
8
2
Orange
9
3
Watermelon
10
4
Banana
What I'd like to have is a query with numbers of occurrences of colors and fruits for each person:
RESULTS
| name| count_colors | count_fruits |
|:----:|:----:|:-------:|
|John|3|2|
|Albert|1|3|
|Joanna|3|2|
|Mike|2|1|
|Norton|1|1|
I'm trying to use this query, but it returns some inconsistent numbers:
SELECT
TABLE_PEOPLE.name AS name,
COUNT(TABLE_COLOR.people_id) AS count_colors,
COUNT(TABLE_FRUIT.people_id) AS count_fruits
FROM TABLE_PEOPLE
LEFT JOIN TABLE_COLOR ON TABLE_COLOR.people_id = TABLE_PEOPLE.id
LEFT JOIN TABLE_FRUIT ON TABLE_FRUIT.people_id = TABLE_PEOPLE.id
GROUP BY TABLE_PEOPLE.id
ORDER BY TABLE_PEOPLE.id
Any idea?
The issue with your current query is that you are using the COUNT function to count the number of occurrences of people_id in TABLE_COLOR and TABLE_FRUIT. However, this will not give you the correct result because the COUNT function will only return the number of non-NULL values, and since you are using LEFT JOIN, all of the people_id values will be non-NULL, so COUNT will always return the same value for each person.
To fix this issue, you can use a subquery in your SELECT clause to count the number of occurrences of colors and fruits for each person:
SELECT
TABLE_PEOPLE.name AS name,
(SELECT COUNT(*) FROM TABLE_COLOR WHERE TABLE_COLOR.people_id = TABLE_PEOPLE.id) AS count_colors,
(SELECT COUNT(*) FROM TABLE_FRUIT WHERE TABLE_FRUIT.people_id = TABLE_PEOPLE.id) AS count_fruits
FROM TABLE_PEOPLE
ORDER BY TABLE_PEOPLE.id
This will return the correct number of occurrences of colors and fruits for each person.
You should join to subqueries which find the various counts:
SELECT
p.name,
COALESCE(c.cnt, 0) AS count_colors,
COALESCE(f.cnt, 0) AS count_fruits
FROM TABLE_PEOPLE p
LEFT JOIN
(
SELECT people_id, COUNT(*) AS cnt
FROM TABLE_COLOR
GROUP BY people_id
) c
ON c.people_id = p.id
LEFT JOIN
(
SELECT people_id, COUNT(*) AS cnt
FROM TABLE_FRUIT
GROUP BY people_id
) f
ON f.people_id = p.id
ORDER BY
p.id;
Your exact problem is happening because of the double join to the colors and fruits table, which results in multiplication of records.
I have store table, product table and a store-product table showing their relationship.
*store table*
store_id name ...
1 store1
2 store2
3 store3
*product table*
product_id name ...
1 product1
2 product2
3 product3
*store-product table*
id store_id product_id
1 1 1
2 1 2
3 2 3
4 3 1
5 3 2
6 3 3
Once products are given, I want to get stores that are selling those products.
i.e: If the given product is 1, then stores 1, 3 should be fetched.
If the given products are 1, 2, 3, then only store 3 should be fetched.
You can use group by and having:
select sp.store_id
from store_product sp
where sp.product_id in (1, 2, 3) -- list of products
group by sp.store_id
having count(*) = 3; -- number of elements in list
Shortened table names, but something like this?
select store.name from pt
join spt on spt.product_id = pt.id
join st on spt.store_id = st.id
where st.product_id = 1
my query select p.idProduct from customer_order c, product p where idProduct not in (select c.product_id from customer_order)
table 1 product
1 cooker 500
2 Frying Pane 600
3 Spoon 110
table 2 customerOrder
Name product id
Sohaib 1
Sohaib 2
Ammar 2
Max 2
want to get item not in Customer order table which is product id
After Executing Query Mentioned Above i get
Product id
2
3
1
3
1
3
1
3
You can use not in or not exists:
select p.*
from products p
where not exists (select 1
from customerOrder co
where co.productid = p.id
);
i have 2 tables as following.
User
id name
---------------
1 john
2 raju
3 manu
4 raghu
friendtable
id userid recvId
------------------------
1 1 2
2 1 3
3 2 3
4 3 4
Is it possible to filter users by their friends count from these tables.Please help me.
For eg :- range >=3 will result : john,manu
range >3 and range <2 will result : raju
range <2 result : raghu
Do a UNION ALL to get all id's from friendstable in one column. Join users table with that result.
Do a GROUP BY, adjust HAVING to decide what to return, e.g. at least 3 times etc.
select u.name
from users
join (select userid as id from friendtable
union all
select recvId as id from friendtable) f
on u.id = f.id
group by u.name
having count(*) >= 3
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recvid>=3
SELECT name FROM user a,friendtable b WHERE a.id=b.id and b.recvid>3 AND b.recid<2
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recid<2
Database structure
Table 'applicants'
id org_id team_id
1 1 1
Table 'teams'
id name
1 Test
Table 'teams_members'
id team_id user_id
1 1 1
2 1 2
Table 'users_playeraccounts'
id user_id summoner_id rank_solo
1 1 1 5
2 1 2 8
3 2 3 7
select sum(rank_solo) as rank_sum,
max(rank_solo) as highest_rank,
count(tt.id) as members,
t.name,
o.team_id
from applicants o
join teams t on o.team_id = t.id
join teams_members tt on t.id = tt.team_id
join users_playeraccounts p on tt.user_id = p.user_id
where org_id = :org
group by team_id
This offcourse gives me a result like
rank_sum highest_rank members name team_id
20 8 3 Test 1
Is there a way for me to get both the count of members with their playeraccounts aka
If 1 user has 2 it'll be 2
And also a way for me to keep it as 1 so it literally just counts the rows found in teams_members neglecting the entries in users_playeraccounts?
I want to receive both 2 and 3 as a result of my query.
You want to count the distinct number of entries in tt.id, so you can do that like this:
SELECT ... COUNT(DISTINCT tt.id) AS distinct_members ...
Rather than giving you a count of every row that has a non-null tt.id, you'll get a count of the number of unique values.