Joining 4 separate tables with count() - mysql

I'm new to SQL and i've been stuck with this problem.
I have 4 tables. I've filled them with some mock information.
Games Table
|ID |Name |Price |
|---|----------|------|
|1 |TestGame1 |2500 |
|2 |TestGame2 |1500 |
|3 |TestGame3 |3500 |
User Table
|ID |Username |Email |
|---|---------|--------------------|
|1 |TestUser1|testEmail1#email.com|
|2 |TestUser2|testEmail2#email.com|
|3 |TestUser3|testEmail3#email.com|
UserOwnsGame Table
|GameID |UserID |
|-------|-------|
|1 |1 |
|2 |2 |
|1 |2 |
|3 |1 |
|2 |1 |
Review Table
|GameID |UserID |Rating |Comment |LastEdit |
|-------|-------|-------|---------------------------------|----------|
|1 |1 |5.0 |I love this game |2022-04-19|
|1 |2 |4.5 |Came short of a 5.0 |2022-04-19|
|2 |2 |2.7 |Above average but nothing special|2022-04-19|
I want to scan through the data on all tables using a single query and get a table like the following,
GameID
UserID
Username
UserReviewCount
UserGameCount
Rating
Comment
LastEdit
1
1
TestUser1
2
3
5.0
I love this game
2022-04-19
1
2
TestUser1
1
2
4.5
Came short of a 5.0
2022-04-19
2
2
TestUser2
1
2
2.7
Above average but nothing special
2022-04-19
I want it for all reviews in the review table. I've tried multiple times. I can figure out ways to get the data on seperate queries. I can't figure out how to combine it all into one table like this. Especially considering the count().
Here;
UserReviewCount - Number of reviews user has made. Count on Review table.
UserGameCount - Number of games user owns. Count on UserOwnsGame table.
I've been stuck on this for one or two days now. Thank you for your help!

We can use a sub-query to count the number of games owned. We could have used another sub-query to count the number of reviews but, as we are already using the table, it is easier to use the window function count() over.
create table Games(ID int,Name varchar(10),Price int);
insert into Games values(1,'TestGame1',2500 ),(2,'TestGame2',1500 ),(3,'TestGame3',3500 );
create table Users (ID int, Username varchar(10),Email varchar(25));
insert into Users values(1,'TestUser1','testEmail1#email.com'),(2,'TestUser2','testEmail2#email.com'),(3,'TestUser3','testEmail3#email.com');
create table UserOwnsGame (GameID int, UserID int);
insert into UserOwnsGame values(1,1),(2,2),(1,2),(3,1),(2,1);
create table Review (GameID int,UserID int,Rating decimal(3,2),Comment varchar(50),LastEdit date);
insert into Review values(1,1,5.0,'I love this game','2022-04-19'),(1,2,4.5,'Came short of a 5.0','2022-04-19'),(2,2,2.7,'Above average but nothing special','2022-04-19');
select
r.GameID,
u.ID,
u.Username,
count(r.GameID) over (partition by r.UserID)
as UserReviewCount,
uog.number_games UserGamescount,
r.Rating,
r.Comment,
r.LastEdit
from
Users u
join Review r
on u.ID = r.UserID
join (select UserID,count(GameID) number_games
from UserOwnsGame
group by UserID) uog
on u.ID = uog.UserID;
GameID | ID | Username | UserReviewCount | UserGamescount | Rating | Comment | LastEdit
-----: | -: | :-------- | --------------: | -------------: | -----: | :-------------------------------- | :---------
1 | 1 | TestUser1 | 1 | 3 | 5.00 | I love this game | 2022-04-19
1 | 2 | TestUser2 | 2 | 2 | 4.50 | Came short of a 5.0 | 2022-04-19
2 | 2 | TestUser2 | 2 | 2 | 2.70 | Above average but nothing special | 2022-04-19
db<>fiddle here

Related

Sort table by matching record percentage

I have a table with parent_id and percentage_score which is a result of a simple deduplication. My problem now is how can I select the records and arrange them like this:
|id|lastname|firstname|number|parent_id|percentage_score|
--------------------------------------------------------
|1 |holmes | james |11112 | 0 |100% |
|7 |holmes | john |11112 | 1 |80% |
|8 |holmes | james |11113 | 1 |60% |
|4 |sherlock| steve |77777 | 0 |100% |
|9 |sherlock| stove |77777 | 4 |80% |
|12|ketchum | ash |00001 | 0 |100% |
I tried using order by parent_id, percentage_score but that's not even near to what I want.
Your request is confusing because your expected result does not seem to be in a logical order. But, you can use the DESC command to change the priority in the ORDER by clause, like this:
SELECT id, lastname, firstname, number, parent_id, percentage_score
ORDER BY parent_id, percentage_score DESC

Counting whole DB while searching for specific SQL

I have a table in db for customers and their glasses
customer_inventory_tbl:
SELECT * FROM customer_inventory_tbl
+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|2 |Steve |Angular|
|3 |John |Aviator|
|4 |Kevin |Supra |
|5 |Jamie |Oval |
|6 |Ben |Supra |
+-------+-------+-------+
(this is a way more simplified version, haha)
If I view John's record it shows
SELECT * FROM customer_inventory_tbl WHERE name=John
+-------+-------+-------+
|id(pk) | name | spex |
+-------+-------+-------+
|1 |John |Oval |
|3 |John |Aviator|
+-------+-------+-------+
But what I require is when viewing John's record, it to show me
+-------+-------+-------+-----+
|id(pk) | name | spex |count|
+-------+-------+-------+-----+
|1 |John |Oval |2 |
|3 |John |Aviator|1 |
+-------+-------+-------+-----+
That "count" column is the number of records in the database that has "Oval" for instance.
Now that is easy enough, if I wanted to count every record in the db, but how do I get the count of all records whilst looking for a specific name.
I hope this makes sense
select c.*,
(
select count(1)
from customer_inventory_tbl
where spex = c.spex
) "count"
from customer_inventory_tbl c;
As a solution according to above mentioned description please try executing following sql query
SELECT *,(select count(id) from customer_inventory_tbl group by spex)
as count FROM customer_inventory_tbl WHERE name='John'
In above mentioned sql query counter value is being retrieved through subquery with records grouped according to values of spex column using GROUP BY clause.

MYSQL Sorting By Multiple Join Children

I cannot seem to figure out a way to sort sql queries by joined children.
Database Example:
Table: posts
+------+---------+
| id | title |
+------+---------+
| 0 |'title1' |
| 1 |'title2' |
| 2 |'title3' |
+------+---------+
Table: post_meta
+------+---------+----------+---------+
| id | post_id | key | value |
+------+---------+----------+---------+
| 0 |0 |'coolness'| 5 |
| 1 |0 |'desc' |'random' |
| 2 |0 |'author' |'bill' |
| 3 |1 |'coolness'| 2 |
| 4 |1 |'desc ' |'random' |
| 5 |2 |'author' |'joe' |
| 6 |2 |'coolness'| 9 |
+------+---------+----------+---------+
I want a list of posts (or just post ids) ordered by their 'coolness' meta number (asc or desc). I dont know if I should be selecting from the posts table and joining on the meta table, or vise-versa. When I join on the post_meta table I only get data from one of the meta rows, so if I just add an order by post_meta.coolness nothing happens.
Thanks!
If you just want the id, you can use the post_meta table:
select pm.post_id
from post_meta pm
where pm.key = 'coolness'
order by pm.value + 0;
The + 0 is to convert the value (presumably a string) to a number.
If you need other columns related to the post, you can join in the posts table.

Select with 5 tables creating almost duplicate rows

I have a database where I have to get name of the review where is the relevant comment and I also need to get the username of person who made that comment.
In order to do that, I have to go through 5 tables because there is no direct connection from comments tablejamacomments to review table review.
I can get review name by:
joining table revision_user with jamacomments
then joining revision_user table with user table userbase
then joining revision_user table with revision table revision which is just updated review
then joining revision table with review table review
My sql query:
select jamacomment.id, jamacomment.userId, jamacomment.commentText,
userbase.id, userbase.userName,
revision_user.userId, revision_user.revisionId,
revision.id, revision.reviewId, review.id, review.name
from jamacomment
left join revision_user
on jamacomment.userId=revision_user.userId
left join userbase
on revision_user.userId=userbase.id
left join revision
on revision_user.revisionId=revision.id
left join review
on revision.reviewId=review.id
group by jamacomment.id
To maybe clarify some things more clearly:
jamacomment.userId is foreign key userbase.id
revision_user.userId is foreign key to userbase.id ( so it's the same as jamacomment.userId)
revision_user.revisionId is foreign key to revision.id
revision.reviewId is foreign key to review.id
So I can get from jamacomment to revision_user from that to revision and from revision to review.
It leaves me with too many records, where it duplicates some data, but not fully. It is a duplicate to certain point where it gives random revisionId number and the rest of the data is wrong by that too.
By using group by I'm selecting only unique jamacomment.id because there can only be so many rows as there are comments. But It retrieves me with wrong records as I wanted to get. It shows some correct lines, but some with data wich is not that comment data, but different comment data.
Maybe I have incorrect select or some wrong left join or I should use other type of join, anyway, I could use any help, to get the correct data to each comment.
Adding dummy table with data for better understanding
table 'userbase' table 'jamacomment'
id | userName id | userId | commentText
1 | Peter 1 | 2 | First comment review1
2 | Jack 2 | 2 | Second comment review1
3 | Ann 3 | 1 | Comment in first review
4 | 1 | Comment in second review
5 | 1 | Comm in 2nd review 2nd revision
6 | 3 | Comment in review1 2nd revision
table 'revision_user' table 'revision' table 'review'
userId | revisionId id | reviewId | sequence id | name
2 | 1 1 | 1 | 1 1 | review1
2 | 1 2 | 2 | 1 2 | review2
1 | 1 3 | 1 | 2
1 | 2 4 | 2 | 2
1 | 4
3 | 3
Expected result should be:
table 'jamacomment' 'userbase' 'revision_user' 'revision' 'review'
id|userId |commentText |id |userName |userId |revisionId |id |reviewId |sequence|id|name
1 |2 |First comment review1 |2 |Jack |2 |1 |1 |1 |1 |1 |review1
2 |2 |Second comment review1 |2 |Jack |2 |1 |1 |1 |1 |1 |review1
3 |1 |Comment in first review |1 |Peter |1 |1 |1 |1 |1 |1 |review1
4 |1 |Comment in second review |1 |Peter |1 |2 |2 |2 |1 |2 |review2
5 |1 |Comm in 2nd review 2nd revision |1 |Peter |1 |4 |4 |2 |4 |2 |review2
6 |3 |Comment in review1 2nd revision |3 |Ann |3 |3 |3 |1 |2 |1 |review1
Forgot to add info that It supposedly breaks somewhere at revisionId where it makes duplicates of the data to revisionId but in revisionId changes the id to those lines. It adds 3 duplicates to each item. The rest info refers to the incorrect revisionId. I suppose It's 3 duplicates because I have 3 reviews or 3 revisions for one review.
It shows me 128 records without group by. with group by it shows the correct 36 records, but It gets some correct and some incorrect records.
Left join will populate your result try using inner join if you want to get only the matches record found on the table that you are joining.

SQL: Compare if column values in two tables are equal

I'm working on mysql and have two tables with the same schema:
preTrial
|id|accusedId|articleid|
------------------------
|1 | 1 | 1 |
|2 | 1 | 2 |
|3 | 1 | 3 |
|4 | 2 | 1 |
|5 | 2 | 2 |
trial
|id|accusedId|articleid|
------------------------
|1 | 1 | 1 |
|2 | 1 | 2 |
|3 | 2 | 1 |
|4 | 2 | 2 |
I want to get those accusedIds where all the articleIds of the first and the second tables are equal.
The above example should only return the accusedId 2, cause for accusedId 1 there is no articleId 3 in the second table.
I hope you understand what i mean. I'm currently writing my thesis in law, and the the time i was into sql is long gone by. Of course i already did some research, and tried several joins, but i was not able to find a solution. Hopefully you can help me.
Try something like this:
select a.accusedId , sum(a.accusedid) as cnt_a, sum(coalesce(b.accusedId, 0)) as cnt_b
from a left join b on a.accusedId = b.accusedId and a.articleId = b.articleId
group by accusedId
having cnt_a = cnt_b
I haven't even run that, so it might be a little off, but give it a lash. What it's doing is returning zeroes for a row in a not matched by b, so the HAVING clause will filter your grouped results to those where the article counts are equal.