I have a table with persons (personid,name), another table with camps (campid,title) and a third table with camp participations (id,personid,campid).
Now, for a given camp, I need a list of all other camp participations by all persons participating in the current camp.
But I have no idea how to join these tables. I have looked at a lot of other examples, but I can't really seem to get any inspiration from those...
This should work:
SELECT *
FROM PERSONS AS D INNER JOIN CAMP_PARTICIPATIONS AS E ON D.PERSONID = E.PERSONID
INNER JOIN CAMPS AS F ON F.CAMPID = E.CAMPID
WHERE F.CAMPID <> [your_camp] AND A.PERSONID IN (
SELECT A.PERSONID
FROM PERSONS AS A INNER JOIN CAMP_PARTICIPATIONS AS B ON A.PERSONID = B.PERSONID
INNER JOIN CAMPS AS C ON C.CAMPID = B.CAMPID
WHERE C.CAMPID = [your_camp] )
select persons.*,participations.*,camps.* from persons left join participations
on participations.personid=persons.personid
left join camps on camps.campid=participations.campid
where camp.campid=1;
now u can change the campid in where clause and place column name which u want in select clause
Related
Here is my Airport Table:
id code airport
1 MAA Chennai Airport
2 DEL Delhi Airport
Here is my Equipment Table:
id type Desc
1 Q400 Q Series
2 B737 B Series
And Here is my Schedule Table:
id station equipment
1 1 2
2 2 1
Here is my expected result:
id station equipment
1 MAA B737
2 DEL Q400
How can I do this ?
This is what I have tried so far:
select schedule.id, schedule.station, flight_schedules.equipment
inner join airport where schedule.station = airport.code
How can I get the expected result.
You can use INNER JOIN or LEFT JOIN according to your requirements as indicated on the diagram below. On the other hand, you can follow this approach in order to join multiple tables:
SELECT * FROM
A INNER JOIN
B INNER JOIN
C INNER JOIN
D INNER JOIN
E INNER JOIN F
ON F.eid = E.id
ON E.did = D.id
ON D.cid = C.id
ON C.bid = B.id
ON B.aid = A.id
Here is the diagram for understanding JOIN types in MSSQL below:
You can do:
SELECT s.id, a.code AS station, e.type AS equipment FROM Schedule s
LEFT JOIN Airport a ON a.id = s.station
LEFT JOIN Equipment e ON e.id = s.equipment
i dont know which language do you use but i brought this example which uses sql language and may be it works for some other languages or all of them:-
SELECT Schedule.id, Airport.code, Equipment.type
FROM ((Schedule
INNER JOIN Airport ON Schedule.station = Airport.id)
INNER JOIN Shippers ON Schedule.equipment = Equipment.id);
*Hallo there,
here is the right answer, what you want to have.
But your expacted result is wrong, because you put equipmnet id 2 in table schedule. That why the answer look different by me *
The right code
SELECT Sch.[equipment]
,Air.code
,Equ.type
FROM [Test].[dbo].[Scheule] as Sch
INNER JOIN [Test].[dbo].[Airport] as Air
ON Air.id = Sch.station
INNER JOIN [Test].[dbo].[Equipment] as Equ
ON Equ.id = Sch.id
enter image description here
I have a bills table with column customer_type and customer_id fields.
This customer_type tells if the customer is in the customers table or in the users table or in the suppliers table.
I need to create a query with left join according to customer_type.
select c.* from bills b
left join ***b.customer_type*** c on c.id = b.customer_id
You could join all three with necessary condition:
select c.*, u.*, s.* from bills b
left join customers c on c.id = b.customer_id and b.customer_type = 'customers'
left join users u on u.id = b.customer_id and b.customer_type = 'users'
left join suppliers s on s.id = b.customer_id and b.customer_type = 'suppliers'
Then you can take the data that is relevant from the result.
However if there are similar columns in these 3 tables you might want to restructure the database to only store one type of information in one place.
I want to retrieve rows from 3 tables - LikeTable, ClosetTable, FollowTable to display the activities of all the users a person is following. This is for a website. ClosetTable is the table for the user's products. LikeTable is the table for the likes of the products and Followtable for the following and followers.
Currently, I have
SELECT c.user, c.productName
FROM ClosetTable AS c
LEFT JOIN FollowTable AS f ON c.user = f.isFollowed
WHERE f.follows = 'tony'
This returns the rows of the person, 'tony' is following together with the productname. However, I want rows from the LikeTable and FollowTable in the same manner altogether. So can you suggest a way to do only 1 query to get rows from all 3 tables?
You can try something that look like this :
SELECT c.user, c.productName
FROM FollowTable AS f
INNER JOIN ClosetTable AS c ON c.user = f.isFollowed
INNER JOIN LikeTable AS l ON c.user = l.userliked
WHERE f.follows = 'tony'
I do some assumption for fields name since you didn't provide the structures of your tables.
Also, I suggested that you put the FollowTable in the FROM clause and then join it to ClosetTable since you put f.follows = 'tony' in your WHERE clause.
Otherwise, remove the WHERE clause and put the condition in the INNER JOIN.
Something like :
LEFT JOIN FollowTable AS f ON c.user = f.isFollowed AND f.follows = 'tony'
More JOINs:
SELECT c.user, c.productName
FROM ClosetTable AS c
LEFT JOIN FollowTable AS f ON c.user = f.isFollowed
JOIN LikeTable as l on c.user = l.liked
WHERE f.follows = 'tony'
or something like that. You aren't restricted to one JOIN.
Is it possible to INNER JOIN a MySQL query to achieve this result?
I have a table with Strategies and a table with Members. The Strategy table holds the ID of the author that corresponds to their ID in the Member table and the ID of an author that updated the existing author's work. Is it possible to grab a reference to both of these people at the same time? Something like the following, which returns no errors, but also no results...
SELECT * FROM Strategies
INNER JOIN Members AS a
INNER JOIN Members AS b
WHERE Strategies.ID='2'
AND Strategies.AuthorID = a.ID
AND Strategies.UpdateAuthorID = b.ID
Use a LEFT JOIN:
SELECT
s.*,
a.Name AS MemberName,
b.Name AS UpdatedMemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2 ;
If you want them in one column use COALESCE:
SELECT
s.*,
COALESCE(a.Name, b.Name) AS MemberName
FROM Strategies AS s
LEFT JOIN Members AS a ON s.AuthorID = a.ID AND s.ID = 2
LEFT JOIN Members AS b ON s.UpdateAuthorID = b.ID AND s.ID = 2
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
if domain is table name
I have three tables in Mysql that are link together:
Profile (ID, Name, Stuff..)
Contact(ID, ProfileID,desc,Ord)
Address(ID,ProfileID, desc, Ord)
Now I need to select all profile from the profile table, with the “desc” field from Contact and Address where Ord = 1. (this is for a search function where in a table I’ll display the name, main contact info and main Address of a client.
I can currently do this with three separate SQL request:
SELECT Name, ID FROM Profile WHERE name=”bla”
Then in a foreach loop, I’ll run the other two requests:
SELECT ProfileID, desc FROM Contact WHERE ProfileID=MyProfileID AND Ord=1
SELECT ProfileID, desc FROM Address WHERE ProfileID=MyProfileID AND Ord=1
I know you can do multiple SELECT in one query, is there a way I could group all three SELECT into one query?
You should be able to JOIN the tables on the profile.id and the profileid in the other tables.
If you are sure the profileid exists in all three tables, then you can use an INNER JOIN. The INNER JOIN returns matching rows in all of the tables:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
inner join contact c
on p.id = c.profileid
inner join address a
on p.id = a.profileid
where p.name = 'bla'
and c.ord = 1
and a.ord = 1
If you are not sure that you will have matching rows, then you can use a LEFT JOIN:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
left join contact c
on p.id = c.profileid
and c.ord = 1
left join address a
on p.id = a.profileid
and a.ord = 1
where p.name = 'bla'
If you need help learning JOIN syntax, here is a great visual explanation of joins
This query below only selects column when an ID from Profile table has atleast one match on tables: Contact and Address. If one or both of them are nullable, use LEFT JOIN instead of INNER JOIN because LEFT JOIN displays all records from the Left-hand side table regardless if it has a match on other tables or not.
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID
INNER JOIN Address c
ON a.ID = c.ProfileID
WHERE b.ORD = 1 AND
c.ORD = 1 AND
a.Name = 'nameHERE'
The LEFT JOIN version:
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID AND b.ORD = 1
INNER JOIN Address c
ON a.ID = c.ProfileID AND c.ORD = 1
WHERE a.Name = 'nameHERE'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
i have created working demo as your requirement :
The query bellow will retrieve all matching records from the database.its retrieving profile id,name stufff and description of contact tables
select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
inner join contact as c on c.profileid=p.id
inner join address as a on a.profileid=p.id
where p.name="bla" and c.ord=1 and a.ord=1