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?
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)
I'm trying to combine 3+ NOAA GSOD data tables to get the data together in one super table. The I am attempting to JOIN the stations table onto the resultant data and then filter by country.
I've been able to do this for just one table but not for more. Below is my attempt at modifying the code to achieve this. I tried several different modifications with no success :(
SELECT * FROM [bigquery-public-data:noaa_gsod.gsod2016] AS gsod2016,
[bigquery-public-data:noaa_gsod.gsod2015] AS gsod2015 JOIN [bigquery-public-
data:noaa_gsod.stations] AS stations ON gsod2016.stn = stations.USAF AND
gsod2015.stn = stations.USAF WHERE stations.country = "CB"
NOAA GSOD bigquery data:
https://bigquery.cloud.google.com/table/bigquery-public-data:noaa_gsod.gsod2016
Use 1 standard inner join or , (preferably inner join syntax) and ensure the tables are in the correct order, you can't join on a table unless its' been defined above the ON.
SELECT *
FROM [bigquery-public-data:noaa_gsod.stations] AS stations
INNER JOIN [bigquery-public-data:noaa_gsod.gsod2016] AS gsod2016
ON gsod2016.stn = stations.USAF
INNER JOIN [bigquery-public-data:noaa_gsod.gsod2015] AS gsod2015
ON gsod2015.stn = stations.USAF
WHERE stations.country = "CB"
Now all this said did you really mean a join or did you want to UNION ALL
the data and add a year
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 );
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 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