Sum total of table with two related tables - mysql

I'm here with this (I'm sure it is) simple question I can't figure out how to solve.
I have this schema:
With this data:
My expected result is:
For "JOHN NASH":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
JOHN NASH | 10 | 38
For "OSCAR WILDE":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
OSCAR WILDE | 28 | 0
Thanks in advance.

SELECT name, IFNULL(f.total, 0) AS total_fruit, IFNULL(c.total, 0) AS total_cookie
FROM person AS p
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM fruit
GROUP BY person_idperson) AS f
ON p.idperson = f.person_idperson
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM cookie
GROUP BY person_idperson) AS c
ON p.idperson = c.person_idperson

SELECT p.name AS PERSON_NAME,
IFNULL(SUM(f.cost),0) AS TOTAL_FRUIT,
IFNULL(SUM(c.cost),0) AS TOTAL_COOKIE
FROM person AS p
LEFT JOIN fruit as f
ON p.idperson = f.person_idperson
LEFT JOIN cookie as c
ON p.idperson = c.person_idperson
GROUP BY p.idperson

Related

Most sold products in shop transactions

I am trying to build a query to select the most sold products given the number of top products to fetch and the language code. I don't know to do it right so I would appreciate you help.
shop_transactions
id
1
2
3
shop_transaction_products_match
id | shop_transaction_id | product_id | units_bought
1 1 1 4
2 2 2 1
3 3 2 2
products_translations
id | product_id | language_code | name | seo_name
1 1 es Hola hola
2 1 en Hey u hey-u
3 2 es Adiós adios
4 2 en Bye u bye-u
products
id | category_id
1 1
2 2
product_categories
id
1
2
product_categories_translations
id | category_id | language_code | name | seo_name
1 1 es AA aa
2 1 en BB bb
3 2 es CC cc
4 3 en DD dd
The information per row is: name from product_translations, seo_name from product_translations, name from product_categories_translations, seo_name from product_categories_translations
Rows ordered descendant by number products sold in total.
Considering the example information, the result if number of products to fetch is 2 and the language is es, would be:
Hola, hola, AA, aa -> 4 units sold of this product
Adiós, adios, CC, cc -> 3 units sold of this product
Thank you!
Edit:
The code I tried so far... and I am missing some things that I dont know how to do it:
SELECT pt.name, pt.seo_name, pct.name as name_category, pct.seo_name as seo_name_category
From product_translations pt, products p, product_categories pc, product_categories_translations pct
where p.id in (Select product_id from shop_transaction_product_match where shop_transaction_id in
(Select id from shop_transactions)) AND pt.language_code = :language_code AND pct.language_code = :language_code
AND p.category_id = pct.category_id
Try this one:
SELECT DISTINCT
pt.name, pt.seo_name, pct.name, pct.seo_name,
SUM(stpm.units_bought) units_sold
FROM products AS p
LEFT JOIN product_categories AS pc ON p.category_id = pc.id
LEFT JOIN products_translations AS pt ON p.id = pt.product_id AND pt.language_code = 'es'
LEFT JOIN product_categories_translations AS pct ON pc.id = pct.category_id AND pct.language_code = 'es'
LEFT JOIN shop_transaction_products_match AS stpm ON p.id = stpm.product_id
-- Where p.id = 1
GROUP BY p.id
ORDER BY units_sold DESC
limit 2
Instead of using IN it is better to use INNER JOIN.
SELECT products_translations.name,
products_translations.seo_name,
product_categories_translations.name,
product_categories_translations.seo_name,
SUM(shop_transaction_products_match.units_bought) AS Product_count
FROM
shop_transactions
INNER JOIN
shop_transaction_products_match
ON shop_transactions.id=shop_transaction_products_match.shop_transaction_id
INNER JOIN
products_translations
ON products_translations.product_id = shop_transaction_products_match.product_id
INNER JOIN
products
ON products.id = shop_transaction_products_match.product_id
INNER JOIN
product_categories
ON products.category_id = product_categories.id
INNER JOIN
product_categories_translations
ON product_categories_translations.category_id = product_categories.id AND product_categories_translations.language_code = products_translations.language_code
WHERE products_translations.language_code= 'es' GROUP BY products.id
Limit 2
Hope this helps.

Get two usernames from the same team

Below is the query I am using to get the team information. However, I want also display both usernames assorted with the team. The database is setup using two tables. One contains all of the user information and the other (userLeagues) contains a set of ids, such as userID and teamID.
Example - userLeagues
leagueID | userID | teamID
1 1 1
1 5 1
1 10 2
2 1 1
2 8 1
This is result I am trying to achieve.
leagueID | TeamID | userID1| userID1
1 1 1 5
This would then be outputed as :
leagueID | TeamID | userID1 | userID1
1 Mad Racing driver 1 dirver 5
This is the code I have so far which gets the team but how do I get both driers and their username associated with the same team?
SELECT
u.username
, t.teamName
, t.teamImage
/* File Table */
, f.fileType
FROM
userleague ul
INNER JOIN users u ON u.userID = ul.userID
LEFT JOIN teams t ON t.teamID = ul.teamID
LEFT JOIN fileType f ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY ul.teamID
You can do this with min() and max(), if there are only two values:
SELECT t.teamName, t.teamImage, f.fileType,
min(u.username) as user1,
(case when max(u.username) > min(u.username) then max(u.username) end) as user2
FROM userleague ul INNER JOIN
users u
ON u.userID = ul.userID LEFT JOIN
teams t
ON t.teamID = ul.teamID LEFT JOIN
fileType f
ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY t.teamName, t.teamImage, f.fileType;

SQL Query that counts grouped by results not working as intended

My goal is to get a table that counts the correct answers from a game.
For example I want this
| G.Name | E.Action
| game 1 | correctAnswer
| game 1 | correctAnswer
| game 2 | correctAnswer
| game 3 | correctAnswer
| game 3 | correctAnswer
to become this
| G.Name | Count(*)
| game 1 | 2
| game 2 | 1
| game 3 | 2
the problem is that im getting this instead:
| G.Name | Count(*)
| game 1 | 5
| game 2 | 5
| game 3 | 5
where 5 is the sum of 2+1+2
This is my query
SELECT G.Name, Count(*)
FROM enduser EU
INNER JOIN prescription P
ON EU.UserRefID = P.EndUserRefID
INNER JOIN prescriptiongame PG
ON PG.PrescriptionRefID = P.PrescriptionID
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = P.PrescriptionID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE P.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name
How can I solve this issue, when I googled people are using the same method but they get good results
Thanks in advance
I'd bet money that one of your joins is inflating the result. Strip your query down to just the critical tables: prescription, session, and entries. Do you still get excessive results? If not, try adding joins back in, one at a time, until you get over-counting.
Your joins are somehow multiplying the number of rows. This happens when you join tables along different dimensions that are not related to each other. This is a common problem, but I don't understand your data structure so the following is a best-guess.
Your example can be fixed by using count(distinct) on something. So try this:
SELECT G.Name, Count(distinct SessionGameRefID)
Should there be a relationship between session and game?
I wonder if this simplification of your query would fix the problem (it removes the first two tables):
SELECT G.Name, Count(*)
FROM prescriptiongame PG
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = PG.PrescriptionRefID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE PG.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name;
supposing all the joins are needed try this query:
SELECT G.Name, Count(G.Name)
FROM enduser EU
INNER JOIN prescription P
ON EU.UserRefID = P.EndUserRefID
INNER JOIN prescriptiongame PG
ON PG.PrescriptionRefID = P.PrescriptionID
INNER JOIN games G
ON G.GameID = PG.GameRefID
INNER JOIN session S
ON S.PrescriptionRefID = P.PrescriptionID
INNER JOIN entries E
ON E.SessionGameRefID = S.Session
WHERE P.EndUserRefID = 889
AND E.Action = 'correctAnswer'
GROUP BY G.Name
ORDER BY G.Name
select G.Name , count(*) from enduser EU, prescription P,prescriptiongame PG ,
games G ,session S ,entries E where P.EndUserRefID = 889 and E.action='correctAnswer' and (EU.UserRefID = P.EndUserRefID) and (PG.PrescriptionRefID = P.PrescriptionID) and (G.GameID = PG.GameRefID) and (S.PrescriptionRefID = P.PrescriptionID) and (E.SessionGameRefID = S.Session)
GROUP BY G.Name
ORDER BY G.Name

Can I fire two select statements in a single MySQL command and count resulting rows?

Here's my code as is, working, but very slow:
$graded = R::getAll("SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid",
array(':discussion_id' => $discussion['id']));
$total = R::getAll("SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid",
array(':discussion_id' => $discussion['id']));
$percentages[] = count($graded) / count($total) * 100;
$graded represents all rows that have a rating.
$total represents all user participation, regardless of being graded or not.
I'm only interested in the numerical values of the resulting sets, can I combine these two MySQL calls into a single call that returns two numbers graded and total?
If I correctly understand your requirements you can try
SELECT p.userid,
COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = :discussion_id
GROUP BY p.userid
Output:
| USERID | TOTAL | GRADED |
---------------------------
| 1 | 8 | 2 |
| 2 | 4 | 4 |
SQLFiddle
UPDATE: If you just want grand total for all posts and users then
SELECT COUNT(*) total,
COUNT(r.itemid) graded
FROM uv_forum_posts p LEFT JOIN
uv_rating r ON p.id = r.itemid
WHERE p.discussion = 1
Output:
| TOTAL | GRADED |
------------------
| 12 | 6 |
SQLFiddle
You could simply use a query as in the format :
Select
( SELECT posts.id, posts.discussion, rating.rating, rating.itemid
FROM uv_forum_posts posts
JOIN uv_rating rating ON ( posts.id = rating.itemid )
WHERE posts.discussion = :discussion_id
GROUP BY posts.userid,
array(':discussion_id' => $discussion['id']))) gradeCount,
(SELECT posts.userid
FROM uv_forum_posts posts
WHERE posts.discussion = :discussion_id
GROUP BY userid,
array(':discussion_id' => $discussion['id'])))totalCount

Joining multiple tables

I'm rather new to normalizing tables and I'm having some troubles wrapping my head around getting the correct information out of 3 tables. I made an example involving reserving books out of different libraries. I have 3 tables. Books, locations, reservations (listed below):
//SQL query:
$sql =
"SELECT * FROM books
JOIN (location LEFT JOIN reservations ON location.locID = reservations.locID)
ON books.bookID = location.bookID
WHERE location.locID=2
";
and the output I was hoping to achieve if I were to list the books in Campus B:
title |locName |status
Book 1|Campus B|1
Book 2|Campus B|0
Book 3|Campus B|0
Book 4|Campus B|0
Book 5|Campus B|1
For some reason I'm definitely not getting the output I think I should, and I was curious if anyone has some suggestions. I'm sure once I see whats going on I'll understand what I did incorrectly.
table: books
bookID|title
1 | Book 1
2 | Book 2
3 | Book 3
4 | Book 4
5 | Book 5
table: location
locID|locName
1 | campus A
2 | campus B
3 | campus C
table: reservations
bookID|locID|status
1 | 1 | 1
3 | 1 | 1
4 | 1 | 1
1 | 2 | 1
5 | 2 | 1
4 | 3 | 1
5 | 3 | 1
I think this is more in line with what you're looking for:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
WHERE l.locID = 2
This will return a list of the books reserved at the location with locID=2.
In this case I have a LEFT JOIN to preserve your original query, however given your WHERE clause any records with NULL in the location.locID field will not be selected.
Therefore, I could re-write your query with all INNER joins, like so:
SELECT *
FROM books b
JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
WHERE l.locID = 2
Other queries that you might find interesting:
Get ALL books, regardless of whether or not they are reserved anywhere:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
Get ALL locations, regardless of whether or not there are books reserved there:
SELECT *
FROM books b
JOIN reservations r ON b.bookID = r.bookID
RIGHT JOIN location l on r.locID = l.locID
Get ALL books and ALL locations:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
RIGHT JOIN location l on r.locID = l.locID
SELECT
books.title
, location.locName
, IFNULL(reservations.status, 0) status
FROM
books
JOIN location
LEFT JOIN reservations ON (
location.locID = reservations.locID
AND books.bookID = location.bookID
)
WHERE location.locID = 2