MySQL Query Compare a value to get another - mysql

I have one database that contains two tables named 'players' and 'morestats'
In the table 'players' I have the columns 'id' and 'player_name'
In the table 'morestats' I have the columns 'id' and 'stat1', 'stat2', etc..
I have to get the value 'stat1' from the table 'morestats' and then search for the player_name of the table 'players' that is egual to the id of both the tables.
All I have to do is this:
Search for the row that has the same ID from 'morestats' and 'players', then get player name.

SELECT stat1, player_name
FROM morestats
INNER JOIN players
USING(id);
This will display a table with each row containing the value of stat1 and the corresponding player_name.
If you want to filter this table you have to put an WHERE statement before the end of the query. Example: WHERE stat1 = 'valuetosearch';

Related

Set column values of a table based on another table in mysql

I have two tables: Employees and Pharmacies.
In table Employees I have a column name Pharmacy which tells me what pharmacy an employee works at, and also a column with the names of the employees.
In table Pharmacies I have a column of pharmacies and I want to add a new one named Number_of_employees which to contain the number employees from the corresponding pharmacy where he works.
Any ideas how to do this ?
DON'T ADD the column Number_of_employees
you can return Number_of_employees value by query
Select Pharmacies.*, count(Employees.Pharmacy) as Number_of_employees from Pharmacies
join Employees on Pharmacies.id = Employees.Pharmacy
group by Employees.Pharmacy

Get DISTINCT show.name and COUNT of associated seasons

I'm trying to write a SQL statement that outputs a table with the two columns show name and number of seasons. The show column must contain no duplocates and the number of seasons column counts the number of seasons associated with the show entity.
Here are my two tables
Shows Table
id | name
Seasons Table
id | show_id | season_number
Here's what I've tried so far
SELECT DISTINCT shows.name
FROM shows
INNER JOIN seasons on show.id = seasons.show_id;
The above code works for grabbing distinct names but whenever I try adding COUNT(season.id) it breaks.
Any suggestions?
Thanks!
Use group by to aggregate multiple rows with the same name into a group. With count(distinct id) you calculate the number of distinct values of the id column in that group.
SELECT name
, COUNT(DISTINCT seasons.id)
FROM shows
JOIN seasons
ON shows.show_id = seasons.show_id
GROUP BY
name
By the way, I'd expect a season to have a one to many relation to show, not the other way around.

MYSQL - Removing number from One Table in SQL if it exists in another Table

I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)

getting from query the maximum expiration and the amount of Id using SQL

I have a table which called "users" which contain these columns of
id,first_name,last_name,phone,gender,user_type
i have another table which called "entrance_logs" which contain 3 colunms
Id. and user id,expiration
I have another table which called :"gifts" which 2 columns
ID and user ID.
I am tring to create a query which will give me the Maximum of the expiration from the "entrance logs" column and the amount of id from table gifts.
I tried this query and it is not return to me the amount of the table of ID correctly.
SELECT users.first_name,users.last_name,users.phone,users.gender,users.user_type,
count(users.id), MAX(gifts.expiration),MAX(entrance_logs.datetime)
FROM users,gifts,entrance_logs
where
users.id= gifts.user_id
and entrance_logs.user_id=users.id
GROUP BY users.id
According to the question description, count(gifts.id) should be used.
Try this:
SELECT users.first_name,users.last_name,users.phone,users.gender,users.user_type,
count(gifts.id), MAX(gifts.expiration),MAX(entrance_logs.datetime)
FROM users,gifts,entrance_logs
where
users.id= gifts.user_id
and entrance_logs.user_id=users.id
GROUP BY users.id

querying values from several tables mysql

I have 4 tables containing id and names from different fields, and a master table that contains only ids, i need to create a query that return the names.
This is the structure (simplified)
table region = columns id, name
table country = columns id, name
table ethnics = columns id, name
table religion = columns id, name
table master = columns region, country,ethnics, religion
table master contains ONLY ids for each column, and i need to return the names that matches those ids, but i can't create the proper JOIN syntax.
Any hint?
Try this:
select region.name, country.name, ethnics.name, religion.name
from master
join region on (region.id = master.region)
join country on (country.id = master.country)
join ethnics on (ethnics.id = master.ethnics)
join religion on (religion.id = master.religion)
Then you can add any where clauses that you might need to filter the results.