How do I output the mutual results appearing in two queries? - mysql

I have made 2 simple queries which output the forename and surname of people based upon the value of a column "role" in a junction table they are associated with.
Lets say that the output is:
Kevin Baker
Julie Willis
Gregory Hilliard
Joe Swanson
Patrick Day
Bethany Row
for one of the query outputs and:
Kevin Baker
Michael Winter
Ellie Patterson
for the other.
I want to be able to make a query which outputs the values that appear in both of these tables i.e. I want it to output Kevin Baker in this case.
Is there a simple way to do this based upon the two queries I have already made?

I have managed to do it myself. I am not sure if it is the best method but it was simple and works.
I kept the first query the same and added
AND IN()
to the end of the WHERE statement.
I then pasted the second query into the IN statement and altered it to only SELECT the id of the related attributes in the outer query.

If you need an intersect just JOIN these queries:
Select Q1.Name FROM (QUERY1) Q1
JOIN
(QUERY2) Q2 On Query1.Name=Q2.Name

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.

SQL Select i dunno what exactly mean

i have sql table like this
pets playstart endstart
cat 12:10 12:50
dog 13:10 13:50
lion 14:10 14:50
if i select 13:35
result dog
how create sql query for this command?
In answer to the question. A select statement is a statement that aims at retrieving some data. SQL was invented to be a human readable way of interacting with databases. As such you can almost phrase your query as a question.
In this case your question would be "What pet would be played with at 13:35?".
Provided the table is guaranteed to have none-overlapping "playstart" and "playend" values this should work (I don't know what the name of your table is so I've used "yourtable" to represent that).
select pets from yourtable where playstart <='13:35' and playend >='13:35'
Please try this one, I hope this will works for you
select pets from pets where playstart >= '13:10' and endstart <= '13:50'

Use MySQL to Find Duplicates and Display on One Line

I'd like to use MySQL to find duplicates and then display those items as one, combined record. For example, I have two columns: Name and Fruit. A sampling of records might look like this:
Joe - Peaches
Faye - Bananas
Joe - Starfruit
Sam - Apples
I want to display this data as:
Joe - Peaches, Starfruit
Faye - Bananas
Sam - Apples
Is that possible? Could you help me with the MySQL query to start? I'm using VB.NET for my application. Thank you.
Use GROUP_CONCAT for that.
SELECT personName, GROUP_CONCAT(fruitName) fruitList
FROM tableName
GROUP BY personName
You'll want to use group_concat here.
SELECT name, group_concat(fruit)
FROM table
GROUP BY name

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.