I am trying to accomplish something and havent been able to find out if it can be done. I have a table "Events" that has event info. Within that table are ID's for Associations that sponsor that event. Sometimes it is only one and sometimes it can be as many as five. These Associations are in the "Associations" table. Within the Associations table, there are several details about that Association. What I am trying to do is do a query that will search the events table, and get all of the events that are between now and the event date, as well as retrieve the information about each Association that relates to each particular event. Here is the query that I have so far:
SELECT
`Events`.EventID,
`Events`.AssociationID,
`Events`.Association2ID,
`Events`.Association3ID,
`Events`.Association4ID,
`Events`.Association5ID,
`Events`.DateFrom,
`Events`.DateTo,
`Events`.EventName,
`Events`.VenueID,
`Events`.TestnTune,
`Events`.ShownShine,
`Events`.SpecialInfo,
`Events`.OtherInfo,
`Events`.Rating,
`Events`.EventOverlay,
`Events`.HavePictures,
`Events`.IncludeInSchedule,
`Events`.EventURL,
Associations.Active,
Associations.Acronym,
Associations.OrgName,
Associations.WebURL,
Associations.LogoURL,
Associations.AssociationID,
Venues.LocationName,
Venues.Location,
Venues.longetude,
Venues.latitude,
Venues.Directions,
Venues.SitePros,
Venues.SiteCons,
Venues.BasicInfo,
Venues.SiteRating,
Venues.HostedEvents,
Venues.CurrentWeather
FROM
`Events`
LEFT JOIN Associations ON `Events`.AssociationID = Associations.AssociationID AND `Events`.Association2ID = Associations.AssociationID AND `Events`.Association3ID = Associations.AssociationID AND`Events`.Association4ID = Associations.AssociationID AND `Events`.Association5ID = Associations.AssociationID
LEFT JOIN Venues ON `Events`.VenueID = Venues.VenueID
WHERE
`Events`.DateFrom >= NOW()
It looks like you're trying to model an many-to-many relationship by using several 1-n relationships and naming them association1, association2, etc... That's probably not a good idea.
Create a new entity called EventToAssociation (or similar), holding foreign keys to both events, and association. Then, your joins will be a lot easier to create.
In an example query, this would read:
SELECT *
FROM Event e
JOIN EventToAssociation e2a ON e.EventID = e2a.EventID
JOIN Association a ON e2a.AssociationID = a.AssociationID
If changing the schema is not an option, then you'll have to join Association several times to Event, e.g.
SELECT *
FROM Event e
LEFT OUTER JOIN Associations a1 ON e.AssociationID = a1.AssociationID
LEFT OUTER JOIN Associations a2 ON e.Association2ID = a2.AssociationID
LEFT OUTER JOIN Associations a3 ON e.Association3ID = a3.AssociationID
LEFT OUTER JOIN Associations a4 ON e.Association4ID = a4.AssociationID
LEFT OUTER JOIN Associations a5 ON e.Association5ID = a5.AssociationID
Every join you make is between your Events-Table and the same Associations-Table. This is just like an AND-Condition.
Try using Aliases:
LEFT JOIN Associations Asso1 ON `Events`.AssociationID = Asso1.AssociationID
LEFT JOIN Associations Asso2 ON `Events`.Association2ID = Asso2.AssociationID
Related
MySQL table schema: entity (key is entityId), post (key is entityId), photo (key is entityId)
Entity is going to be 1:1 to the combined post+photo tables, as these are basically subcategories of entity
sql request:
SELECT * FROM entity
LEFT JOIN post ON post.entityId = entity.entityId
LEFT JOIN photo ON photo.entityId = entity.entityId
This does return the full schema, but for entities that do not have a photo, the entityId returned is null. Every row that doesnt have a corresponding photo has its entityId set to null.
QUESTION: What is the cleanest way to combine these subgroups while maintaining all the data? I did manage to do it by doing only one, normal join with posts, and then union-ing that with another entity joined with photos, but my solution is very messy and theres probably a better way to do it.
You need to list out the columns explicitly. The problem you are having is the columns have the same name. For example:
SELECT e.*, pid as post_id, ph.id as photo_id
FROM entity e LEFT JOIN
post p
ON p.entityId = e.entityId LEFT JOIN
photo ph
ON ph.entityId = e.entityId;
If the only columns that have duplicate ids are the entityId columns, then you can use the USING clause (assuming your database supports it):
SELECT *
FROM entity e LEFT JOIN
post p
USING (entityId) LEFT JOIN
photo ph
USING (entityId)
SELECT team_with.participant1,team_with.participant2,team_with.participant3
FROM event,team_with
WHERE team_with.for_event_no=event.event_no AND
event.event_no=4 AND
team_with.participant1=9 OR
team_with.participant2=9 OR
team_with.participant3=9;
I have written the particular query, and obtained the required id's in a row. I am not able to modify this query such that, in place of these id's, names connected to the id's are displayed.
The student_detatil table consists of PK(sam_id) and the attribute name.
IDs displayed by the present query are FKs connected to student_detail.sam_id..
It seems like a bad design to multiply columns storing different participants. Consider creating a separate row for each participant and storing them in a table. Your joining logic would also be easier.
Also, please use explicit JOIN syntax - it makes the query clearer and easier to understand by separating join logic with conditions for data retrieval.
Remember that operator AND has a precedence over OR, so that your event.event_no = 4 does not apply to each participant condition. I believe this was a mistake, but you are the one to judge.
As to the query itself, you could apply OR conditions into join, or simply join the student_detail table thrice.
SELECT
s1.name,
s2.name,
s3.name
FROM
event e
INNER JOIN team_with t ON t.for_event_no = e.event_no
LEFT JOIN student_detail s1 ON s1.sam_id = t.participant1
LEFT JOIN student_detail s2 ON s2.sam_id = t.participant2
LEFT JOIN student_detail s3 ON s3.sam_id = t.participant3
WHERE
e.event_no = 4
AND ( t.participant1=9 OR t.participant2=9 OR t.participant3=9 );
So I have these 2 tables:
Objects (idObjects, obj_name)
Relations (idRelations, rel_name, dom_idObjects, cdom_idObjects)
dom and cdom are FK's to the Objects table. They represent a relationship of existing objects.
I am trying to write a query that returns every relation within a certain depht, along with the corresponding object names. So for example if I want every relation within a depht of 1 (a simple relation between 2 objects), I managed to get this to work:
SELECT A.rel_name
,C1.obj_name AS object1_name
,C2.obj_name AS object2_name
FROM relations AS A
INNER JOIN Objects AS C1 ON A.dom_idObjects = C1.idObjects
INNER JOIN Objects AS C2 ON A.cdom_idObjects = C2.idObjects
However, when trying to extend this to a depht of 2 or higher I'm not getting any results (and according to my DB there should be several):
SELECT A.rel_name
,C1.obj_name AS object1_name
,C2.obj_name AS object2_name
,C3.obj_name AS object3_name
,C4.obj_name AS object4_name
FROM relations AS A
INNER JOIN Objects AS C1 ON A.dom_idObjects = C1.idObjects
INNER JOIN Objects AS C2 ON A.cdom_idObjects = C2.idObjects
INNER JOIN Objects AS C3 ON A.dom_idObjects = C2.idObjects
AND A.cdom_idObjects = C3.idObjects
INNER JOIN Objects AS C4 ON A.dom_idObjects = C3.idObjects
AND A.cdom_idObjects = C4.idObjects
I guess I'm missing something. What gives?
I'm using visual studio sql database to inner join 3 tables to one main table.
The main table is Event and the PK_EventsID is linked to the FK_EventsID of the other 3 tables (StudentCompetition, ImmersionTrip, IndustryCollaboration)
I tried using inner join with 2 tables (Event and StudentCompetition) and it works, but when I add another table (Event, StudentCompetition, ImmersionTrip) it does not display anything even though there is an inner join. The column names appear, but the data doesn’t.
Similar column names are startDate, endDate and eventID while the remaining are different. I would also like to know how to group similar column names together
I'm planning to join more then 10 tables together for a search function.
SQL Query
SELECT
Event.eventName,
Event.eventID,
StudentCompetition.competitionName,
ImmersionTrip.immersionName
FROM
Event
INNER JOIN StudentCompetition ON Event.eventID = StudentCompetition.eventID
INNER JOIN ImmersionTrip ON Event.eventID = ImmersionTrip.eventID
I have database with schema on picture below and I need to select everything related to one row (one id) of [letaky]. That means the related [zamestnanci], every related [obsah] and every [knihy] in it.
This is the first time i used relations in database and i have no idea how to make such a select.
Use JOIN ... ON:
SELECT *
FROM zamestnanci
JOIN lekaty ON lekaty.zamestnanciid = zamestnanci.id
JOIN obsah ON obsah.idletaku = lekaty.id
JOIN knihy ON knihy.id = obsah.idknihy
WHERE letaky.id = 123
You may also want to consider whether you need INNER JOIN, LEFT JOIN or RIGHT JOIN for each of these joins. The difference between these JOINs is described in many other questions on StackOverflow, for example this one:
SQL Join Differences