Jump to content

MySQL - JOIN MULTIPLE TABLES


AndieB

Recommended Posts

Hello all Gurus!

I need help with a QUERY SYNTAX for MySQL.

I have the following TABLES in my database:

[b]incident[/b]
[b]iinfo[/b]
[b]iplatform[/b]
[b]iserver[/b]
[b]icomponent[/b]

In each of the TABLES there is a COLUMN with name name 'id'. This is the COLUMN that "logically" is the reference point. Now, I would like to get a query that fetches ALL the columns from ALL the tables above based on the existens 'id' in the "main" table which is the [b]incident[/b] TABLE.

How do I write such a QUERY SYNTAX?

I appreciate any kind of help!

Best regards,

Andreas
Link to comment
Share on other sites

I'm not sure I understand... they all have FKs to the main "incident" table?

Then maybe:

[code]
SELECT incident.*, iinfo.*, iplatform.*, iserver.*, icomponent.*
FROM incident
LEFT JOIN iinfo ON iinfo.incident_id = incident.id
LEFT JOIN iplatform ON iplatform.incident_id = incident.id
LEFT JOIN iserver ON iserver.incident_id = incident.id
LEFT JOIN icomponent ON icomponent.incident_id = incident.id
[/code]
Link to comment
Share on other sites

Thank you for this!

The only thing I had to correct was the following:

[b]From[/b]
LEFT JOIN iinfo ON iinfo.incident_id = incident.id
[b]to[/b]
LEFT JOIN iinfo on iinfo.id = incident.id

The only thing I still want to solve, is that the COLUMN 'id' is now shown from every TABLE in my RESULT. Since I already have the 'id' from the TABLE incident, I don't need it any further in the rest of the RESULT. Is this possible?

Again, thank you so much for your time and effort!

--Andreas


[quote author=fenway link=topic=113320.msg460734#msg460734 date=1162323317]
I'm not sure I understand... they all have FKs to the main "incident" table?

Then maybe:

[code]
SELECT incident.*, iinfo.*, iplatform.*, iserver.*, icomponent.*
FROM incident
LEFT JOIN iinfo ON iinfo.incident_id = incident.id
LEFT JOIN iplatform ON iplatform.incident_id = incident.id
LEFT JOIN iserver ON iserver.incident_id = incident.id
LEFT JOIN icomponent ON icomponent.incident_id = incident.id
[/code]
[/quote]
Link to comment
Share on other sites

[quote author=fenway link=topic=113320.msg461338#msg461338 date=1162413678]
Well, the "solution" is just to ask for the columns you want.. there's no way to exclude columns per se.
[/quote]

Ok I understand. I've done that now.
Now my QUERY looks like this:

[code]SELECT incident.*,
iplatform.platform,
iserver.server,
icomponent.component,
iinfo.cust_impact,
iinfo.followup,
iinfo.vinstsr,
iinfo.incidentreport,
iinfo.gsp,
ihistory.descrip
FROM incident
LEFT JOIN iinfo ON iinfo.id = incident.id
LEFT JOIN iplatform ON iplatform.id = incident.id
LEFT JOIN iserver ON iserver.id = incident.id
LEFT JOIN icomponent ON icomponent.id = incident.id
LEFT JOIN ihistory on ihistory.id = incident.id
ORDER BY incident.idate ASC[/code]

In the TABLE ihistory the design looks like this:
[code]
COLUMN | DATATYPE | NULL | STANDARD VALUE
-----------------------------------------------
id | int(11) | NO | 0

datentime | datetime | NO | 0000-00-00 00:00:00

descrip | longtext | Yes | NULL

[/code]

The PRIMARY KEYS for this TABLE are: id AND datentime

When I run the QUERY written above, the list will display the same id twice, because there are [b]two[/b] rows in the ihistory table. If I only want to show [b]one[/b] row, since there may not be duplicates in the incident TABLE, and the FIRST row in the ihistory TABLE, how does my QUERY need to be written?

Thank you again for your time and effort helping me out!

[b]Andreas[/b]
Link to comment
Share on other sites

Hmm... OK, I understand what you mean. Then it is to the practical... meaning, writing the QUERY...  ;)

How would it look with a SUBQUERY in my existing example?

Thank you again for your time and effort!

--[b]Andreas[/b]
Link to comment
Share on other sites

Hmmmm... *thinking*


Well, there are ONE unique entry from the incident TABLE. The only table that can have MORE rows one ID is the ihistory table. All other tables can only have ONE row.

ie. incident.id = 1, means that it can only be  ONE *.id = 1 in the other TABLES, excluding the ihistory table. As you could see the PRIMARY KEYS for the ihistory TABLE are id & datentime.

Hmm, do I explain good or am I totally gone in explanation??

Best regards,

[b]Andreas[/b]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.