Select statement for multiple foreign keys - mysql

I have these Tables :
Livestream
----------
id_livestream int primary key,
name_match varchar(255),
date_match varchar(255),
time_match varchar(255),
league_type int,
tour varchar(255),
stadium int,
id_team1 int,
id_team2 int,
live_video varchar(255),
Team
------
id_team int primary key,
name_team varchar(255),
image_team varchar(255)
League
------
id_league int primary key,
name_league varchar(255)
staduim
-------
id_stadium int primary key,
name_stadium varchar(255)
I am using this sql query to get the data from the tables as below :
select id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
from Livestream,League,staduim,IsLive
where Livestream.league_type=League.id_league and Livestream.stadium=staduim.id_stadium
What i am getting :
id_livestream|name_match|date_match|time_match|name_league|tour|stadium
65 BarcaMatch 9/5/2017 20:45 League1 22 CampNou
This query is going well but i don t know how to alter this query to :
select the name team,image_team from Team for both id_team1,id_team2 from Livestream
UPDATED : i have searched and found this query to get what i want but i can t add it to my first query :
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
UPDATE 2 :
Please i need help

Simply rewrite the whole query in explizite join Format:
SELECT
lm.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
lm.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2,
id_livestream,name_match,time_match,date_match,name_league,tour,name_stadium,
live_video
FROM Livestream lm
INNER JOIN Team t1
ON lm.id_team1 = t1.id_team
INNER JOIN Team t2
ON lm.id_team2 = t2.id_team
JOIN League on lm.league_type=League.league
JOIN staduim on lm.stadium=staduim.id_stadium

Related

MySQL query with multiple full joins

I have a query which gets all the jobs from a database, some jobs don't have a languagepair and I need to get those too while still getting the languagepair information for jobs witch have a languagepair, I understand this is done with a full join but full joins do not exist in mySQL, I read about it and I need to do some sort of UNION.
If I get NULLS as source & target for jobs that do not have a languagepair it is good.
This is the query I have at the moment:
SELECT jobName, source.name AS source, target.name AS target FROM (
(SELECT jobs.name AS jobName, lp.sourceId, lp.targetId FROM jobs **JOIN languagePairs** lp
ON lp.id = jobs.languagePairId)
UNION
(SELECT jobs.name AS jobName, lp.sourceId, lp.targetId FROM collectiveJobs JOIN jobs ON jobs.id = collectiveJobs.jobId
**JOIN languagePairs lp** on jobs.languagePairId = lp.id
WHERE collectiveJobs.freelancerId = 1)
) AS jobs **JOIN languages** source ON source.id = sourceId **JOIN languages** target ON target.id = targetId;
I think but I am not sure the full joins need to happen at the bold joins. There also needs to be some sort of checking for null (I think) in the query.
Off course I could do this programmatically but it would be nice to have 1 query for it.
DB schema:
create table languages
(
id int auto_increment primary key,
name varchar(255) not null
)
create table languagePairs
(
id int auto_increment
primary key,
sourceId int not null,
targetId int not null,
constraint languagePair_sourceId_targetId_uindex
unique (sourceId, targetId),
constraint languagePair_language_id_fk_source
foreign key (sourceId) references languages (id),
constraint languagePair_language_id_fk_target
foreign key (targetId) references languages (id)
)
create table jobs
(
id int auto_increment
primary key,
name varchar(255) null,
freelancerId int null,
languagePairId int null,
constraint jobs_freelancers_id_fk
foreign key (freelancerId) references freelancers (id),
constraint jobs_languagePairs_id_fk
foreign key (languagePairId) references languagePairs (id)
)
create table collectiveJobs
(
id int auto_increment
primary key,
jobId int not null,
freelancerId int not null,
constraint collectiveJobs_freelancerId_jobId_uindex
unique (freelancerId, jobId),
constraint collectiveJobs_freelancers_id_fk
foreign key (freelancerId) references freelancers (id),
constraint collectiveJobs_jobs_id_fk
foreign key (jobId) references jobs (id)
)
create table freelancers
(
id int auto_increment primary key
)
Sample data:
INSERT INTO datamundi.jobs (id, name, freelancerId, languagePairId) VALUES (1, 'Job 1', 1, 1);
INSERT INTO datamundi.jobs (id, name, freelancerId, languagePairId) VALUES (2, 'Job 2', 1, null);
If I execute the query only Job 1 gets shown.
MySQL version on development machine: mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
MySQL version on production server: mysql Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)
All help is truly appreciated.
I can't really delve into your specific example, but the good news is you are using MySQL 8.x. The workaround for a FULL OUTER JOIN between two tables (a and b) in MySQL is:
select * from a left join b on <predicate>
union
select * from a right join b on <predicate>
Now if you need to join complex selects instead of simple tables, them CTEs come to your rescue. For example, if the left side were a comple SELECT you would do:
with s as ( <complex-select-here> )
select * from s left join b on <predicate>
union
select * from s right join b on <predicate>
If both are complex SELECTs then:
with s as ( <complex-select-here> ),
t as ( <complex-select-here> )
select * from s left join t on <predicate>
union
select * from s right join t on <predicate>
No sweat.
This works with all LEFT joins, I am sorry, I should have tried first.
SELECT jobName, source.name AS source, target.name AS target FROM (
(SELECT jobs.name AS jobName, lp.sourceId, lp.targetId FROM jobs LEFT JOIN languagePairs lp
ON jobs.languagePairId = lp.id)
UNION
(SELECT jobs.name AS jobName, lp.sourceId, lp.targetId FROM collectiveJobs JOIN jobs ON jobs.id = collectiveJobs.jobId
LEFT JOIN languagePairs lp on jobs.languagePairId = lp.id
WHERE collectiveJobs.freelancerId = 1)
) AS jobs LEFT JOIN languages source ON source.id = sourceId LEFT JOIN languages target ON target.id = targetId;
Not sure why I taught I needed a FULL JOIN...

How to intersect between a view and a table in mysql?

I've a view and a table. I'm inserting data into table on the basis of that view. I want that view to eliminate same data that is already inserted into table.
This is the view which is named view_1.
CREATE VIEW view_1 AS
SELECT
y.employee_registration_id,
x.employee_first_name,
x.employee_last_name,
x.employee_email,
y.employee_current_salary_amount,
(SELECT
x.salary_of_year_month_YearMonth
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth,
(SELECT
CONCAT(MONTHNAME(x.salary_of_year_month_YearMonth),
'-',
YEAR(x.salary_of_year_month_YearMonth))
FROM
salary_of_years_months x
LEFT JOIN
status_salary_of_years_months y ON x.salary_of_year_month_id = y.salary_of_year_month_id
WHERE
y.year_month_status = 'Ongoing') AS salary_of_year_month_YearMonth_MonthYear
FROM
basic_info_employees x
LEFT JOIN
current_salary_employees y ON x.employee_registration_id = y.employee_registration_id
WHERE
y.employee_current_salary_amount > 0;
This is the table.
CREATE TABLE `salary_tracker_employees` (
serial_no INT(4) NOT NULL AUTO_INCREMENT,
employee_registration_id INT(4),
employee_first_name VARCHAR(30),
employee_last_name VARCHAR(30),
employee_email VARCHAR(30),
salary_of_year_month_YearMonth VARCHAR(50),
employee_current_salary_amount DOUBLE(8 , 2 ),
PRIMARY KEY (serial_no),
UNIQUE (employee_registration_id , salary_of_year_month_YearMonth)
);
I want to get a view that will intersect the between view_1 and salary_tracker_employees.

How to using where and join in mysql?

I have two tables;
database tftube;
table tftube_video;
database tfmember;
table member;
create table member(
no int primary key AUTO_INCREMENT,
member_no int,
name varchar(15) not null
);
create table tftube_reply(
no int primary key AUTO_INCREMENT,
member_no int,
video_name varchar(100)
);
I need to
1.tfmember.member.member_no equals tftube.tftube_reply.member_no
2.video_name=10.
3.Result variables are name, no, member_no,video_name.
So I try to this way.
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
but it's error
please help me
p.s tfmember.member.member_no contains tftube.tftube_reply.member_no.
Correct query will be:
select a.name from member a join tftube_reply b on a.no = b.member_no and b.video_name='10';
You should learn more about query before posting to ST.
Refer below link:
https://www.tutorialspoint.com/sql/index.htm
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
You join with table a auto increment col with table b member_no and video_name is varchar type that's why use '10' in where condition
Correct Query
select a.name from tfmember.member a join tftube.tftube_reply b on a.member_no = b.member_no where b.video_name='10';

MySQL query -- retrieve data from two different years

I cannot seem to get this MySQL query right. My table contains yearly inventory data for retail stores. Here's the table schema:
CREATE TABLE IF NOT EXISTS `inventory_data` (
inventory_id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
store_id smallint unsigned NOT NULL,
inventory_year smallint unsigned NOT NULL,
shortage_dollars decimal(10,2) unsigned NOT NULL
)
engine=INNODB;
Every store is assigned to a district which in this table (some non-relevant fields removed):
CREATE TABLE IF NOT EXISTS `stores` (
store_id smallint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
district_id smallint unsigned not null
)
engine=INNODB;
I want to be able to retrieve the shortage dollar amounts for two given years for all the stores within a given district. Inventory data for each store is only added to the inventory_data table when the inventory is completed, so not all stores within a district will all be represented all the time.
This query works to return inventory data for all stores within a given district for a given year (ex: stores in district 1 for 2012):
SELECT stores.store_id, inventory_data.shortage_dollars
FROM stores
LEFT JOIN inventory_data ON (stores.store_id = inventory_data.store_id)
AND inventory_data.inventory_year = 2012
WHERE stores.district_id = 1
But, I need to be able to get data for stores within a district for two years, such that the data looks something close to this:
store_id | yr2011 | yr2012
For the specific result format that you need, you may try the following query:
SELECT `s`.`store_id`, `i`.`shortage_dollars` AS `yr2011`, `i1`.`shortage_dollars` AS `yr2012`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
AND `i`.`inventory_year` = 2011
LEFT JOIN `inventory_data` `i1` ON `s`.`store_id` = `i1`.`store_id`
AND `i1`.`inventory_year` = 2012
WHERE `s`.`district_id` = 1
Alternatively, you may as well try the next simpler query.
SELECT `s`.`store_id`, `i`.`inventory_year`, `i`.`shortage_dollars`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
WHERE `s`.`district_id` = 1
AND `i`.`inventory_year` IN (2011, 2012)
ORDER BY `s`.`store_id`, `i`.`inventory_year`
Hope it helps!
SELECT
stores.store_id,
inventory_data.inventory_year
inventory_data.shortage_dollars
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
;
or
SELECT
stores.store_id,
GROUP_CONCAT(inventory_data.shortage_dollars) dollars_per_year
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
GROUP BY stores.id,inventory_year;

SQL: How to join a view with a table?

UPDATED:
I am using MySQL statement to create a view:
I need to show Editors First Name, Last Name and the City if they shipped more than 50 books. The three tables I have are:
create table editors (
ed_id char(11),
ed_lname varchar(20),
ed_fname varchar(20),
ed_pos varchar(12),
phone varchar(10),
address varchar(30),
city varchar(20),
state char(2),
zip char(5),
ed_boss char(11));
create table titleditors (
ed_id char(11),
title_id char(6),
ed_ord integer);
create table salesdetails (
sonum integer,
qty_ordered integer,
qty_shipped integer,
title_id char(6),
date_shipped date);
Can anyone tell me what code would be to create this result?
I didn't make the tables, I just have to work with what I was given.
Antiquated syntax (note the intermixing of join conditions and filter conditions):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors, salesdetails, editors
WHERE
titleditors.title_id = salesdetails.title_id
AND editors.ed_id = titleditors.ed_id
AND salesdetails.qty_ordered > 50
Modern syntax (join conditions and filter conditions are separate):
CREATE VIEW qtyorderedview AS
SELECT
salesdetails.title_id, salesdetails.qty_shipped,
editors.ed_id, editors.ed_lname, editors.ed_fname, editors.city
FROM
titleditors
INNER JOIN salesdetails ON titleditors.title_id = salesdetails.title_id
INNER JOIN editors ON editors.ed_id = titleditors.ed_id
WHERE
salesdetails.qty_ordered > 50
Joins against views work exactly like joins against tables. Just use the view name in place of a regular table name.
SELECT e.*
FROM (
SELECT DISTINCT te.ed_id
FROM (
SELECT title_id
FROM sales_details
GROUP BY
title_id
HAVING SUM(qty_shipped) > 50
) t
JOIN titleditors te
ON te.title_id = t.title_id
) te
JOIN editors e
ON e.ed_id = te.ed_id