简体   繁体   中英

Join more than two tables

I am trying to join 2 tables and in 1 table there may or may not have the corresponding values.but i need to join the table and list the fields as null.

I have tried to join the tables as left join.but If two entries is there in secoond table corresponding to the value of first table,then first table data is displayed twice.

I need to display only one time if there is two data in another table or there is no data in another table,but it should display as null.

Here is my sql query.

        SELECT *,incident.incident_id as incidentid
        FROM register_incident AS incident
        LEFT JOIN incident_images AS im ON im.incident_id= 
        incident.incident_id
        WHERE incident.state='Active'

I need to display only one time each data if there is no corresponding rows in another table,but the fields in the second table list as null.

If there is more than one row in another table,then also display each row in first table one time with one entry in the second table.

you can use select distinct for get only one row eg (limiting the select to indicent_id ,, but you can add the distinctcolumn you need ):

 SELECT *,incident.incident_id as incidentid
    FROM register_incident AS incident
    LEFT JOIN (
      select distinct incident_id from 
      incident_images
      WHERE incident.state='Active' ) t   ON t.incident_id= incident.incident_id

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