How to call different data from one column via intermediary table? - mysql

At first I have to say that I am an absolute SQL newbie.
I have two SQL tables. The second one is an intermediary m:n table.
Table: people
id
name
1
Yoda
2
Anakin
3
Luke
4
Obi-Wan
Table: master-padawan_relation
master_id
padawan_id
1
3
4
2
master-padawan_relation.master_id and master-padawan_relation.padawan_id both refer to people.id.
Now I want a query that shows the following data:
Master
Padawan
Yoda
Luke
My query looks currently like this but it lacks the integration of the Masters name:
SELECT
people.name AS Padawan
FROM
people
LEFT JOIN
master-padawan_relation ON master-padawan_relation.padawan_id = people.id
WHERE
people.id = 3
I don't know how to get that Masters name in there, because both names are in the same column (people.name).
I also managed to get the masters id, but I really need that name.
Appreciate every help! Thank you :)

Related

How to Count Duplicated Names in a Varchar/Text Column in MySQL/Any SQL

So, this is the situation: I have a CSV file who looks like this:
show_id title cast
1 Batman Robert Pattinson, Collin Farrel, Zoë Kravitz
2 Twilight Robert Pattinson, Kristen Stewart
3 Ava Jessica Chastain, Collin Farrel
What I need to do is open this CSV in a Python function, do some stuff to fix white spaces and that.
Then I need to upload that in a SQL database, (whatever I want, but I choose MySQL), no problem with that.
PROBLEM
My main issue is that then I need (because mi challenge said that) to create a Query to count how many times an actor appears in all the movies in the list. So, in this case the query should display something like this:
Actor Amount_of_movies
Robert Pattinson 2
Collin Farrel 2
Zoë Kravitz 1
Kristen Stewart 1
Jessica Chastain 1
As you can see I don't have a name to search with LIKE or CONTAINS. So, how can I do that? Because, in the CSV, the list of actors per movie has, well, more than 1 actor, and I save them in a varchar or text type in the database, so each row has more than one actor.
Or should I create another table with actors and foreing keys to movies? Or is something than I can't do in MySQL but in other SQL is possible?
If you look for something performance efficient you should rather split the data (create 3 tables in total - movies, actors, casts) and connect actors with movies using the casts, then write simple sql with some joins like:
Select actors.name as Actor, count(movies.title) as Amount_of_movies from actors,
inner join cast on cast.actor_id = actors.actor_id
inner join movies on movies.movie_id = cast.movie_id;
You are able to do it another way using https://sebhastian.com/mysql-split-string/ or using psql / plsql and stored procedure. I would just split the data if it's possible.
Cheers.

Beginner Help: Looking for Member Names in diffrent projects to see in which projects they are working together

I am very new to SQL. I have done some basic "select from where ..." queries but I struggle with my current project.
Lets say this is my source table:
Project Involved
1 Harald
1 Kerstin
1 Peter
1 Christian
1 Lisa
1 Linda
2 Sören
2 Schmidt
2 Jörg
2 Robert
2 Harald
2 Lisa
My question should be fairly simple. The input is the name "Lisa" and "Harald". I want to know "Which projects are Lisa and Harald involved in"
If this is super easy and cannot understand why I ask such easy thing: provide me with a link where this is explained and ill read trough it myself, just not so sure what exactly to look for so I thought this was a faster way to get started :)
You can solve this in many ways, but here is the primary way I would solve it... but start simply for what projects LISA is associated with... This prevents looking at what could be 1000s of projects / people but if Lisa is only associated with 5 (or 2 in this case), why query against all of them..
Select
p1.project
from
project p1
where
p1.involved = 'Lisa'
So this lists projects 1 & 2 (obviously your short sample of data. Now, that we know this works, I would just JOIN again for Harold and the same project as this.
Select
p1.project
from
project p1
join project p2
on p1.project = p2.project
AND p2.involved = 'Harold'
where
p1.involved = 'Lisa'
Ensure you have an index on (involved, project) to help optimize the query
Additionally, others may propose to do a group by and having clause based on both parties you are interested in.... something like
select
p1.project
from
project p1
where
p1.involved in ( 'Lisa', 'Harold')
group by
p1.project
having
count(*) = 2
This basically says to the engine. Give me each project where either Lisa or Harold exist. But, by applying a group by I only want the project to show once so I don't see duplicates. The HAVING clause tells how many you EXPECT to have per project, and since you are asking for 2 possible names, and want both of them, the HAVING COUNT(*) is 2 so you know BOTH are included.
I've named table as person, so this is the example
SELECT
p1.project
FROM
person p1,
person p2
WHERE
p1.name = 'Harald'
AND p2.name = 'Lisa'
AND p1.project = p2.project
To see the projects which both Lisa and Harald are involed in:
SELECT P1.Project FROM
(SELECT Project FROM MyTable WHERE Involved = 'Lisa') P1
INNER JOIN
(SELECT Project FROM MyTable WHERE Involved = 'Harald') P2
WHERE P1.Project = P2.Project

mysql combining records from one table

I have a single table that uses test# as the primary key. Here is what that table looks like:
Test# Name VerbalScore readingScore Notes
1 Bobby 92 Good job
2 Bobby 40 You Suck Bobby
The problem is I want to view and be able to see when there are multiple verbal scores for the same Name (so be able to see if the person took the same test more than once).
I want to have some kind of select statement to get this result from the above table:
1 Bobby 92 40 Good job, You Suck Bobby
Is that possible?
I am not totally sure I understand what you mean by "see when there are multiple verbal scores" but with mysql 5+, try
SELECT
Name,
GROUP_CONCAT(VerbalScore),
GROUP_CONCAT(readingScore),
GROUP_CONCAT(Notes)
FROM
myTable
GROUP BY
Name;
GROUP_CONCAT is a mysql specific grouping function.

Selecting data from two tables

Hello everybody
Well my question is about sql commands...
If I have 2 tables with the same number of columns and the same fieldnames (e.g: A(n,name,date) and B(n,name,date))
In the website, I want to retrieve data from both tables and display them in order by date descendent.
(The use of two tables is due to difference in tables database or server,or just the use of every table.. sometimes there's a need to display both tables in one order)
exemple
table Sport_news(N_event,Title,Texte,Date)
table International_news(N_event,Title,Texte,Date)
Display:
Christiano Ronaldo ... 2011/25/01
christiano ronaldo is one of the famous...
Barack Obama president of the USA... 2011/24/01
Barak obama........
The arsenal has... 2011/23/01
Chamakh, player of arsenal is anger.....
I hope that the idea is clear : and thank you!
You want UNION
select a.name,a.date
from table1 a
where ...
UNION ALL
select b.name,b.date
from table2 b
where ...
order by 2 desc
When you use a UNION, you specify the order by with column numbers instead of names.

MySQL: How to pull information from multiple tables based on information in other tables?

Ok, I have 5 tables which I need to pull information from based on one variable.
gameinfo
id | name | platforminfoid
gamerinfo
id | name | contact | tag
platforminfo
id | name | abbreviation
rosterinfo
id | name | gameinfoid
rosters
id | gamerinfoid | rosterinfoid
The 1 variable would be gamerinfo.id, which would then pull all relevant data from gamerinfo, which would pull all relevant data from rosters, which would pull all relevant data from rosterinfo, which would pull all relevant data from gameinfo, which would then pull all relevant data from platforminfo.
Basically it breaks down like this:
gamerinfo contains the gamers basic
information.
rosterinfo contains basic information about the rosters
(ie name and the game the roster is
aimed towards)
rosters contains the actual link from the gamer to the
different rosters (gamers can be on
multiple rosters)
gameinfo contains basic information about the games (ie
name and platform)
platform info contains information about the
different platforms the games are
played on (it is possible for a game
to be played on multiple platforms)
I am pretty new to SQL queries involving JOINs and UNIONs and such, usually I would just break it up into multiple queries but I thought there has to be a better way, so after looking around the net, I couldn't find (or maybe I just couldn't understand what I was looking at) what I was looking for. If anyone can point me in the right direction I would be most grateful.
There is nothing wrong with querying the required data step-by-step. If you use JOINs in your SQL over 5 tables, we sure to have useful indexes on all important columns. Also, this could create a lot of duplicate data:
Imagine this: You need 1 record from gamerinfo, maybe 3 of gameinfo, 4 ouf of rosters and both 3 out of the remaining two tables. This would give you a result of 1*3*4*3*3 = 108 records, which will look like this:
ID Col2 Col3
1 1 1
1 1 2
1 1 3
1 2 1
... ... ...
You can see that you would fetch the ID 108 times, even if you only need it once. So my advice would be to stick with mostly single, simple queries to get the data you need.
There is no need for UNION just multiple JOINs should do the work
SELECT gameinfo.id AS g_id, gameinfo.name AS g_name, platforminfoid.name AS p_name, platforminfoid.abbreviation AS p_abb, rosterinfo.name AS r_name
FROM gameinfo
LEFT JOIN platforminfo ON gameinfo.platforminfoid = platforminfo.id
LEFT JOIN rosters ON rosters.gameinfoid = gameinfo.id
LEFT JOIN rosterinfo ON rosterinfo.id = rosters.rosterinfoid
WHERE gameinfo.id = XXXX
this should pull all info about game based on game id
indexing on all id(s) gameinfoid, platformid, rosterinfoid will help on performance