简体   繁体   中英

MySQL Inner Join, selecting from multiple tables

I'm really struggling to get my head around this. I am trying to run a SELECT query from multiple tables.

This is what I have so far that doesn't work;

SELECT jira_issues.*, session_set.* FROM jira_issues, session_set
INNER JOIN reports on jira_issues.report_id = reports.id 
    WHERE jira_issues.report_id = 648

I have other tables (session_set, report_device) which has a ReportID and report_id column respectively.

I have a report table which has a Primary Key id . In the other tables the report.id key is linked with foreign keys.

Ultimately what I am trying to achieve is this: I have an entry in the reports table with an id of 648. In the other tables (jira_issues, report_device, session_set), I also have entries which has a foreign key linked to the report id in the report table.

I want to run one SELECT Query to query the tables (jira_issues, report_device and session_set) and get all the data from them based on the report.id.

Thanks!

What about this:

SELECT * FROM jira_issues ji
    LEFT JOIN session_set ss ON ji.report_id = ss.ReportID
    LEFT JOIN report_device rd ON rd.report_id = ji.report_id
WHERE ji.report_id = 648;

Just say "no" to commas in the from clause. Always use explicit join syntax:

SELECT ji.*, session_set.*
FROM jira_issues ji inner join
     reports r
     on ji.report_id = r.id  inner join
     session_set ss
     on ss.ReportId = r.report_id
WHERE ji.report_id = 648;

If some of the tables might have no corresponding rows, you might want left outer join instead of inner join .

Kindly try this out. You may get syntax error.

SELECT a. , b. FROM jira_issues a, session_set b, reports c Where a.report_id = c.id and b.report_id = c.id AND a.report_id = 648

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM