Two foreign keys referencing same column - mysql

I have two Tables : Livestream and Team.
Livestream:
id_livestream int primary key
id_team1 int
id_team2 int
// I had already referenced these two columns to Team id_team column
Team:
id_team int primary key
name_team varchar(40)
image_team varchar(255)
I want to select name_team from Team for both referenced columns in Livestream..
as example i want to show something like:
id_team1| name of team1| image of team1 | id_team2| name of team 2| image of team2

You can generate the output you want by simply doing two joins from the Livestream table to the Team table:
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
I assume here that every team appearing in Livestream will have an entry somewhere in the Team table. If this be not the case, and you don't want NULL values appearing in your result set, then you can switch to using a LEFT JOIN along with COALESCE().

Try this:
SELECT
ls.id_team1,
t1.name_team AS name_team_1,
t1.image_team AS image_team_1,
ls.id_team2,
t2.name_team AS name_team_2,
t2.image_team AS image_team_2
FROM Livestream ls
INNER JOIN Team t1
ON ls.id_team1 = t1.id_team
INNER JOIN Team t2
ON ls.id_team2 = t2.id_team
Inner join two times with Team does the trick.

Related

How to Join three tables with one reference in the query in MySQL

Hi is there a way to join three tables like this but I can't change the reference of the column code.id at the query because I am creating a dynamic query for a search filter for this reference
Here is the code looks like
there are 4 tables bill, expense_distribution, item_distribution, project_code
columns in the bill - id, name, ...etc
columns in the expense_distribution - id, bill_id, project_code_id, ...etc
columns in the item_distribution - id, bill_id, project_code_id, ...etc
columns in the project_code - id, name, ...etc
SELECT b.id, code.name FROM bill b
LEFT JOIN expense_distribution exp ON b.id=exp.bill_id
LEFT JOIN project_code code ON exp.project_code_id=code.id
LEFT JOIN item_distribution itm ON b.id=itm.bill_id
LEFT JOIN project_code code ON itm.project_code_id=code.id
I can't use the query with two times project_code code but I want code.id for both item and expense distributions because of the filter.
can someone guide me for the best approach to do that, I am using JPQL for the code in Java
There are multiple ways to approach this.
One way you can achieve this is by providing different aliases for same table and coalesce the fields.
SELECT b.id, COALESCE(c1.name, c2.name) as name FROM bill b
LEFT JOIN expense_distribution exp ON b.id=exp.bill_id
LEFT JOIN item_distribution itm ON b.id=itm.bill_id
LEFT JOIN project_code c1 ON exp.project_code_id=c1.id
LEFT JOIN project_code c2 ON itm.project_code_id=c2.id;
Another approach would be, change the firsy and last two lines to
SELECT b.id, code.name FROM bill b
LEFT JOIN expense_distribution exp ON b.id=exp.bill_id
LEFT JOIN item_distribution itm ON b.id=itm.bill_id
LEFT JOIN project_code code ON COALESCE(itm.project_code_id, exp.project_code_id)=code.id;
Third, change the last line from above to
LEFT JOIN project_code c1 ON exp.project_code_id=code.id OR itm.project_code_id=code.id
This is the tables creation:
CREATE TABLE bill(
id int(11),
name varchar(10)
);
CREATE TABLE project_code(
id int(11),
name varchar(10)
);
CREATE TABLE expense_distribution(
id int(11),
bill_id int(11),
project_code_id int(11),
name varchar(10)
);
CREATE TABLE item_distribution(
id int(11),
bill_id int(11),
project_code_id int(11),
name varchar(10)
);
Based on these tables you can query the following query:
SELECT b.id, codes.name FROM bill b
LEFT JOIN expense_distribution exp ON b.id=exp.bill_id
LEFT JOIN project_code codes ON exp.project_code_id=codes.id
LEFT JOIN item_distribution itm ON b.id=itm.bill_id and itm.project_code_id =codes.id

Two tables. One contains the school info and the other personal. A quire that picks students who are from CA and like either skateboard or soccer.

What currently happens is it just selects students from CA and students who like to skateboard. I need it to return only students who are both from CA and play soccer.
SELECT *
FROM schooldata a
INNER JOIN studentinfo b
ON b.schooldata_id = a.id
WHERE a.state = "ca"
AND ( activity = "soccer"
OR activity = "skateboard" )
You will have to do inner join based on lastname and firstname column ex:-b.lastname=a.lastname and b.firstname=a.firstname . ideally you should be maintaining primary key column of type integer in schooldata table and its foreign key reference in studentinfo and join based on those columns.
You should use join clauses. And I think base on your question. Inner Join is the best clause you should use.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

Microsoft Access SQL Joins

List all players and their ages for their associated current team for teams that have a manager.
Here are my 3 Tables and their attributes
PLAYER
PLAYERNUM Primary Key
PLAYERNAME
PlayerAge
TEAM
TEAMNUM Primary Key
TEAMNAME
TeamCity
MgrNum
AFFILIATION
PLAYERNUM Primary Key/Foreign Key
TEAMNUM Primary Key/Foreign Key
AffilYrs
AffilBatAvg
AffilDateStart Foreign Key
AffilDateEnd
AffilCurrentTeam
Here is my SQL:
SELECT PLAYERNAME, PlayerAge
FROM PLAYER INNER JOIN AFFILIATION
ON PLAYER.PLAYERNUM = AFFILIATION.PLAYERNUM
INNER JOIN TEAM
ON AFFILIATION.TEAMNUM = TEAM.TEAMNUM
WHERE MgrNum IS NOT NULL;
I am given the error missing operator in query expression
Perhaps it should be;
[NotTested]
SELECT PLAYERNAME, PlayerAge
FROM (PLAYER INNER JOIN AFFILIATION
ON PLAYER.PLAYERNUM = AFFILIATION.PLAYERNUM)
INNER JOIN TEAM
ON AFFILIATION.TEAMNUM = TEAM.TEAMNUM
WHERE MgrNum IS NOT NULL;
GROUP BY PLAYER.PLAYERNAME, PLAYER.PlayerAge
You should use parentheses for multiple joins in access.

How to Select data trough 2 tables with ONE Query

Every client has their own project but not all of them has project.
I want to select all clients that has project but I do not know how to do it!
I get all clients with this query:
SELECT `clients` FROM `reps` WHERE `clients` != ''
My goal is to get data which has only project
These are my database tables:
Table Clients: (Table name = reps)
1 id varchar(12) // example: stckvrflw
2 ctitle varchar(100) // example: StackOverflow
Table Projects: (Table name = verkocht)
1 id varchar(11) // example: 1
2 title varchar(100) // example: This is an Example
Do you have a solution on my problem?
You have the client in the milestones table, so doesn't this do what you want?
SELECT DISTINCT m.client
FROM `milestones` m;
You have to join the projects and the milestones tables. Since you gave no information on your database shema, the field names have to be adjusted. You also have to check for deleted or hidden flags if you have some:
SELECT c.* FROM clients c
INNER JOIN projects p ON p.client = c.id
INNER JOIN milestones m ON m.project = p.id
GROUP BY c.id

Remove results from SELECT by conditions on multiple columns

So I have this feed of products
id man sup product
1 1 1 MacBook
2 1 2 iMac
3 2 1 Windows
4 2 2 Office
and then tables with manufacturers
id manufacturer
1 Apple
2 Microsoft
and suppliers
id supplier
1 TechData
2 Westcoast
Then, for some reasons, I don't want to show a manufacturer's products by a certain supplier, i.e.:
id man sup comment
1 2 1 TechData aren't allowed to sell Microsoft
2 1 2 hide all Apple products from Westcoast
Is there a way, in pure SQL, to show only the products which fall through my filter, in this case MacBook and Office? I believe this isn's just a WHERE NOT (x AND y) as the result will list the remaining combinations.
Thanks a lot!
This is just a variation on Return row only if value doesn't exist, except you're joining on two columns.
SELECT p.product, m.manufacturer, s.supplier
FROM products AS p
JOIN manufacturers AS m ON m.id = p.man
JOIN suppliers AS s ON s.id = p.sup
LEFT JOIN filter AS f ON p.man = f.man AND p.sup = f.sup
WHERE f.id IS NULL
DEMO
You can try this, mate:
First, create the temporary/real container for your custom manufacturer-supplier filter:
-- pivot temp/real table for suppliers - manufacturers
DROP TEMPORARY TABLE IF EXISTS `manufacturers_suppliers`;
CREATE TEMPORARY TABLE `manufacturers_suppliers` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`manufacturer_id` INT(11) UNSIGNED,
`supplier_id` INT(11) UNSIGNED,
PRIMARY KEY (`id`),
UNIQUE KEY (`manufacturer_id` ASC, `supplier_id` ASC)
);
-- populate pivot table
INSERT INTO `manufacturers_suppliers` (`manufacturer_id`, `supplier_id`)
VALUES
-- TechData aren't allowed to sell Microsoft
(2, 1),
-- hide all Apple products from Westcoast
(1, 2);
After that, using the content of the container, you only need to create a standard query for your result set.
-- create result
SELECT
p.id, p.product, # show product detail
s.id, s.supplier, # show supplier detail
m.id, m.manufacturer # show manufacturer detail
FROM
products p
INNER JOIN suppliers s ON s.id = p.sup
INNER JOIN manufacturers m ON m.id = p.man
LEFT JOIN `manufacturers_suppliers` ms ON
ms.manufacturer_id = man.id
AND ms.supplier_id = sup.id
WHERE ms.id IS NULL;
So that every time you have an update to your filter, you only update records not the actual query script. Cheers