Joining multiple sql tables to find common value - mysql

I have two sql tables that I am trying to join together. I'm trying to make it so that a userid from my first table- table "video" and the posted_by from table "review" are joined so that the output lists a user pair where that they always gave each other "five” for the "star" column
I was working with this statement, but it's not yielding any results.
There are only two users in the tables that fit the requirement: Jstark#gmail.com and cmanley#gmail.com
Query:
SELECT U1.userid, U2.userid
FROM video U1, video U2
WHERE U1.userid < U2.userid
(SELECT COUNT(*)
FROM video
WHERE userid = U1.userid) =
(SELECT COUNT(J.star) FROM video J, review R
WHERE J.userid = U1.userid AND J.star = R.star AND R.posted_by = U2.userid AND R.star = "5")
review
(SELECT COUNT(*)
FROM video
WHERE userid = U2.userid) =
(SELECT COUNT(J1.star
FROM video J1, review R1
WHERE J1.userid = U2.userid AND J1.star = R1.star AND R1.posted_by = U1.userid AND R1.star = "5");
Table video:
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| video_url | video_name | video_description | video_subject | video_city | video_tags | star | date | userid | reviewed_by |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://www.youtube.com/watch?v=xGa9AbKO-4s | Nice video of Detroit | Watch as someone shows you Detroit | Downtown Detroit | Detroit | lake | 5 | 2019-11-29 | atticus#yahoo.com | Michael#aol.com |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/5KE7ppUIPhQ | Sacaomento Forest | A nice forest in Sacromento | Forest in Sacramento | Sacramento | lake | 3 | 2019-11-30 | Don#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/5KE7ppUIPhQ1 | Sacromento Forest | A nice forest in Sacromento | Forest in Sacromento | Sacromento | lake | 5 | 2019-12-01 | Don#yahoo.com | |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/BnbE6QzKY-I | Great video of downtown Dallas | See a tour of downtown Dallas | Downtown Dallas | Dallas | lake | 0 | 2019-12-01 | john#gmail.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/HGvwO8qq8FI | Tour of Cape Cod | See a tour of Cape Code | View of Cape Code | Cape Code | lake | 5 | 2019-12-02 | Michael#aol.com | atticus#yahoo.com |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/HiZXABMNCUY | Tour of Manhattan | See Manhattan on the big screen | Downtown New york | Manhattan | lake | 2 | 2019-11-27 | Don#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/ORoyGEgvfXY | Nice video of Orlando | See the appeals of Orlando | Downtown Orlando | Orlando | lake | 4 | 2019-11-28 | Hhinata#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/pKwuW06NvXM | Scenic Downtown Cincinatti | Take the backroutes through Cincinatti | Take the backroutes through Cincinatties | Cincinatti | lake | 3 | 2019-11-26 | Jstark#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/QSwvg9Rv2EI | Tour of Chicago | Let us bring Chicago to you | Downtown Chicago | Chicago | lake | 2 | 2019-11-25 | john#gmail.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/qY02yEvqFio | Downtown Detroit | Tour of Downtown Detroit | Downtown Detroit | Detroit | lake | 5 | 2019-11-27 | Megan#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
| https://youtu.be/_QH_tdYR3iI | Great video of Lake Erie | See the great lakes up close | Michigan great lakes | Traverse City | lake | 0 | 2019-11-27 | cmanley#yahoo.com | NULL |
+---------------------------------------------+--------------------------------+----------------------------------------+------------------------------------------+---------------+------------+------+------------+-------------------+-------------------+
Table review:
+--------------------------------+--------------------------+-------------+-------------------+
| video_name | review_comments | review_star | posted_by |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | cool dude | 2 | john#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Tour of Chicago | cool video | 5 | Jstark#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Tour of Cape Code | could have been better | 3 | Megan#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | good video | 5 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | great video | 0 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | great video! | 2 | don#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of Lake Erie | i really didnt like this | 0 | cmanley#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | it was terrible | 0 | Don#gmail.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Great video of downtown Dallas | Nice van | 4 | Hhinata#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Detroit | nice video | 5 | test#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Sacramento Forest | okay video | 3 | atticus#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Scenic Downtown Cincinatti | terrible video | 0 | Michael#aol.com |
+--------------------------------+--------------------------+-------------+-------------------+
| Nice video of Orlando | wonderful video | 5 | Hhinata#yahoo.com |
+--------------------------------+--------------------------+-------------+-------------------+

To solve the problem I'm going to start at some basic queries and work towards the answer asked.
Start by collating the the videos and the reviews with a join
SELECT v.video_name, AVG(review_star)
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.video_name
Then we can look at the average star rating for reviewers:
SELECT r.posted_by, AVG(review_star)
FROM review r
GROUP BY r.posted_by
Take a step further and group by the poster/review pair:
SELECT v.userid, r.posted_by, AVG(review_star)
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.userid, r.posted_by
HAVING allows a post filter on this. All 5 star rating will have an average of 5.
SELECT v.userid, r.posted_by, AVG(review_star) as avg
FROM video v
JOIN review r
USING (video_name)
GROUP BY v.userid, r.posted_by
HAVING avg = 5

Related

Join same field multiple times in table

I have this 2 tables and I am trying to join both of them multiple times but failed. Below are the tables.
Table ccaSubjects:
+------------+----------+
| ccaSubject | ccaPrice |
+------------+----------+
| Chess | 100 |
| Badminton | 300 |
| Dancing | 200 |
| Singing | 200 |
| Football | 250 |
| Fitness | 600 |
| Robotics | 1000 |
+------------+----------+
Table rispEnrollment
+--------------------+-----------+-----------+----------+
| studentIdentifier | firstCCA | secondCCA | thirdCCA |
+--------------------+-----------+-----------+----------+
| elly#example.com | Robotics | Singing | Dancing |
| mike#example.com | Chess | Singing | Robotics |
| tom#example.com | Badminton | Dancing | Chess |
| peter#example.com | Football | Fitness | Robotics |
| andrew#example.com | Robotics | Singing | Chess |
+--------------------+-----------+-----------+----------+
I would like my output to be like:
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
| studentIdentifier | firstCCA | secondCCA | thirdCCA | CCA1price | CCA2price | CCA3price |
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
| elly#example.com | Robotics | Singing | Dancing | 1000 | 200 | 200 |
| mike#example.com | Chess | Singing | Robotics | 100 | 200 | 1000 |
| tom#example.com | Badminton | Dancing | Chess | 300 | 200 | 100 |
| peter#example.com | Football | Fitness | Robotics | 250 | 600 | 1000 |
| andrew#example.com | Robotics | Singing | Chess | 1000 | 200 | 100 |
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
From my code, I am only able to use an inner join one time and get the CCA1price, and I cannot get cca2price and cca3price anymore because the error keeps saying Same aliases.
You may join the rispEnrollment table to the ccaSubjects table as many times as you need. In this case, you may join three times to bring in the price columns for each of the three subject columns.
SELECT
t1.studentIdentifier,
t1.firstCCA,
t1.secondCCA,
t1.thirdCCA,
t2.ccaPrice AS CCA1price,
t3.ccaPrice AS CCA2price,
t4.ccaPrice AS CCA3price
FROM rispEnrollment t1
LEFT JOIN ccaSubjects t2
ON t1.firstCCA = t2.ccaSubject
LEFT JOIN ccaSubjects t3
ON t1.secondCCA = t3.ccaSubject
LEFT JOIN ccaSubjects t4
ON t1.thirdCCA = t4.ccaSubject;
Note that I use left joins here just in case the rispEnrollment table might have a subject which does not match to anything in the ccaSubjects table.

Query to get especific data from custom search

Im coding a custom search box to find starcraft games, but i have no idea how to do the query. FOR EXAMPLE an user search "Player: daniel, who played: zerg vs a terran player on 'x' map"
So i have this tables:
Table games
+------------+----------+-------------+----------+
| game_id | map | match | winner |
+------------+----------+-------------+----------+
| 7 | y | 1v1 | 1 |
| 8 | x | 1v1v1 | 2 |
| 9 | w | 1v1v1 | 3 |
| 10 | x | 1v1 | 1 |
+------------+----------+-------------+----------+
Table players
+------------+----------+-------------+----------+
| game_id | player |Civilization | team |
+------------+----------+-------------+----------+
| 7 | Arturo | protos | 1 |
| 7 | Daniel | zerg | 2 |
| 8 | Ale | Terran | 1 |
| 8 | Maria | Protos | 2 |
| 8 | Daniel | zerg | 3 |
| 9 | Pablo | zerg | 1 |
| 9 | Ale | protos | 2 |
| 9 | Maria | protos | 3 |
| 10 | Daniel | zerg | 1 |
| 10 | Oscar | terran | 2 |
+------------+----------+-------------+----------+
With the correct query i must get :
Game_id: 8, 1v1v1, daniel (zerg) vs maria (protos) vs ale (terran), map: x
Game_id:10, 1v1, daniel (zerg) vs oscar (terran), map: x
The problem here is how i get the game WHERE exist a daniel player AND zerg civ AND WHERE an oponent have a terran civ in the same game_id??! And also the game is played in x map?
PD: As you notice each game can have different size of players. Please help. Very much appreciated
try this select g.game_id, GROUP_CONCAT(CONCAT(p.player,'(', p.Civilization, ')-',g.map) SEPARATOR ' vs ') from games as g join players p on p.game_id = g.game_id where g.map = 'x' group by g.game_id;

Selecting from 3 tables

I have the following tables below.
athletes
+------------+-----------------+---------------+-------------------------+
| idATHLETES | ATHLETENAME | TEAMS_idTEAMS | TEAMS_COUNTRY_idCOUNTRY |
+------------+-----------------+---------------+-------------------------+
| JG | JUSTIN GATLIN | USA-TF-MEN | USA |
| MS | MARIA SHARAPOVA | RUS-WTA | RUS |
| SW | SERENA WILLIAMS | USA-WTA | USA |
| UB | USAIN BOLT | JAM-TF-MEN | JAM |
| VW | VENUS WILLIAMS | USA-WTA | USA |
+------------+-----------------+---------------+-------------------------+
EVENTS
+------------+---------------+---------------------+------------------------+----------------------------------+
| idEVENTS | EVENTNAME | ATHLETES_idATHLETES | ATHLETES_TEAMS_idTEAMS | ATHLETES_TEAMS_COUNTRY_idCOUNTRY |
+------------+---------------+---------------------+------------------------+----------------------------------+
| ATH | ATHLETICS | JG | USA-TF-MEN | USA |
| ATH | ATHLETICS | UB | JAM-TF-MEN | JAM |
| TEN | TENNIS | MS | RUS-WTA | RUS |
| TEN | TENNIS | VW | USA-WTA | USA |
| TEN-DOUBLE | TENNIS DOUBLE | SW | USA-WTA | USA |
| TEN-DOUBLE | TENNIS DOUBLE | VW | USA-WTA | USA |
+------------+---------------+---------------------+------------------------+----------------------------------+
RESULTS
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+
| idRESULTS | STATUS | MEDALS | EVENTS_idEVENTS | EVENTS_ATHLETES_idATHLETES | EVENTS_ATHLETES_TEAMS_idTEAMS | EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+
| results1 | DID-NOT-WIN | SILVER | TEN | MS | RUS-WTA | RUS |
| results1 | WON | GOLD | TEN | VW | USA-WTA | USA |
| results2 | DID-NOT-WIN | BRONZE | ATH | JG | USA-TF-MEN | USA |
| results2 | WON | GOLD | ATH | UB | JAM-TF-MEN | JAM |
| results3 | WON | GOLD | TEN-DOUBLE | SW | USA-WTA | USA |
| results3 | WON | GOLD | TEN-DOUBLE | VW | USA-WTA | USA |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+
How can I get a list Athletes who participate in more than one Event
and won at least one of them
and won none of them
I have come out with this code below but this returns the wrong output ?
SELECT idATHLETES, ATHLETENAME, EVENTNAME FROM athletes
JOIN EVENTS ON idATHLETES = ATHLETES_idATHLETES
JOIN RESULTS ON events.ATHLETES_idATHLETES = RESULTS.EVENTS_ATHLETES_idATHLETES
WHERE idEVENTS >=2 AND STATUS = 'WON'
Athletes who participated in more than one Event and won at least one of them:
select a.idATHLETES as id,a.ATHLETENAME as Name,count(*) as evtCount,
SUM(CASE WHEN r.STATUS = 'WON' THEN 1 ELSE 0 END) as victoryCount
from athletes a
join EVENTS e
on e.ATHLETES_idATHLETES = a.idATHLETES and e.ATHLETES_TEAMS_idTEAMS = a.TEAMS_idTEAMS and e.ATHLETES_TEAMS_COUNTRY_idCOUNTRY = a.TEAMS_COUNTRY_idCOUNTRY
join RESULTS r
on r.EVENTS_idEVENTS=e.idEVENTS and r.EVENTS_ATHLETES_idATHLETES=e.ATHLETES_idATHLETES -- etc
group by a.idATHLETES,a.ATHLETENAME
having evtCount>1 and victoryCount>0
order by a.idATHLETES,a.ATHLETENAME;
Results:
+----+----------------+----------+--------------+
| id | Name | evtCount | victoryCount |
+----+----------------+----------+--------------+
| VW | VENUS WILLIAMS | 2 | 2 |
+----+----------------+----------+--------------+
Athletes who participated in more than one Event and won none:
select a.idATHLETES as id,a.ATHLETENAME as Name,count(*) as evtCount,
SUM(CASE WHEN r.STATUS = 'WON' THEN 1 ELSE 0 END) as victoryCount
from athletes a
join EVENTS e
on e.ATHLETES_idATHLETES = a.idATHLETES and e.ATHLETES_TEAMS_idTEAMS = a.TEAMS_idTEAMS and e.ATHLETES_TEAMS_COUNTRY_idCOUNTRY = a.TEAMS_COUNTRY_idCOUNTRY
join RESULTS r
on r.EVENTS_idEVENTS=e.idEVENTS and r.EVENTS_ATHLETES_idATHLETES=e.ATHLETES_idATHLETES -- etc
group by a.idATHLETES,a.ATHLETENAME
having evtCount>1 and victoryCount=0
order by a.idATHLETES,a.ATHLETENAME;
Results:
no rows returned, you lack data for that
Notes
This is just a group by with having, and conditional aggregation (plus really messed up column names) :p
You can use having to get the records where there are at least two events and status = 'won' at least once.
SELECT idATHLETES, ATHLETENAME, EVENTNAME FROM athletes
JOIN EVENTS ON idATHLETES = ATHLETES_idATHLETES
JOIN RESULTS ON events.ATHLETES_idATHLETES = RESULTS.EVENTS_ATHLETES_idATHLETES AND RESULTS.Status = 'WON'
HAVING COUNT(idEVENTS) >=2 AND COUNT(STATUS) >= 1

Selecting various values from an event

I have got these 3 tables below, i want to list all athletes who won Gold
Athletes
+------------+-----------------+
| athletesID | athletesName |
+------------+-----------------+
| jg | justin gatlin |
| ms | maria sharapova |
| ub | usain bolt |
| vw | venus williams |
+------------+-----------------+
events
+---------+-----------+---------------------+
| eventID | eventName | athletes_athletesID |
+---------+-----------+---------------------+
| ev1 | tennis | ms |
| ev1 | tennis | vw |
| ev2 | mens 100m | jg |
| ev2 | mens 100m | ub |
+---------+-----------+---------------------+
results
+-----------+--------+----------------+----------------------------+
| resultsID | Medal | events_eventID | events_athletes_athletesID |
+-----------+--------+----------------+----------------------------+
| results1 | silver | ev1 | ms |
| results1 | Gold | ev1 | vw |
| results2 | silver | ev2 | jg |
| results2 | Gold | ev2 | ub |
+-----------+--------+----------------+----------------------------+
I have used this code below so far but this lists all the silver medalists as having won Gold
SELECT
athletesID, athletesName, medal
FROM
myoly.athletes
JOIN
myoly.events ON athletes.athletesID = events.athletes_athletesID
JOIN
myoly.results ON myoly.events.eventID = myoly.results.events_eventID
WHERE
medal = 'gold';
How can I list all athletes who won Gold only ?
SELECT
a.athletesID,
a.athletesName,
r.medal
FROM results r
LEFT JOIN athletes a
ON a.athletesID = r.events_athletes_athletesID
WHERE r.medal = 'Gold';
If you remove your WHERE medal = 'gold' and look at your results, it should give you a clue about the problem. This is often the best way to troubleshoot.
Anyway, you need to join results to athletes on athleteID instead of eventID. This is because you're concerned with which athletes won gold not which events had a gold medal.
SELECT a.athletesID,
a.athletesName,
r.medal
FROM athletes a
INNER JOIN results r ON a.athletesID = r.events_athletes_athletesID
WHERE r.medal = 'Gold'

Display the Object with the most equal entries

Hi I want to display the country(Land in german) which haves the most museums.
My table looks like:
+-----------+----------------------+-------------------+--------------------------+---------------+
| MuseumsNR | Name | Stadt | Land | Hauptstadt |
+-----------+----------------------+-------------------+--------------------------+---------------+
| 1 | Museum of Modern Art | New York | United States of America | Washington DC |
| 2 | Kunstmuseum | Bern | Schweiz | Bern |
| 3 | Musée Picasso | Paris | Frankreich | Paris |
| 4 | Städel | Frankfurt am Main | Deutschland | Berlin |
| 5 | Museum Ludwig | Köln | Deutschland | Berlin |
+-----------+----------------------+-------------------+--------------------------+---------------+
So my desired output should be:
+--------------------------+------+
| Land |Anzahl|
+--------------------------+------+
| Deutschland | 2 |
+--------------------------+------+
This is what i´ve tried:
SELECT Land, COUNT(Name) Name from Museum order by Name desc;
THe Output:
+--------------------------+------+
| Land | Name |
+--------------------------+------+
| United States of America | 5 |
+--------------------------+------+
Thanks in advance for your help!
SELECT Land, count(*) as Anzahl
FROM yourTable
GROUP BY Land
ORDER BY Anzahl
DESC LIMIT 1;`
Only tested in PostgreSQL, but should be close enough in MySQL.