简体   繁体   中英

How to write a SQL query to get data from several tables?

I need next data from different tables in one report

I need to choose all consultants info from consultant.table

first_name & last_name & email & phone from passport.table

在此处输入图片说明

I have passport_id in users table

在此处输入图片说明

Number of consultant and his level from main table of query consultant.table

Use this sql query to get your result:

  SELECT  a.first_name, 
      a.last_name, 
      a.email, 
      a.phone, 
      b.level,
      b.user_id
      from passport a 
      INNER JOIN user c 
      ON a.id=c.passport_id
      INNER JOIN consultant b 
      ON b.user_id=c.id

Check this SQL Fiddle for output

Try to do an inner or left join. Something like this:

SELECT a.first_name, a.last_name, a.email, a.phone, b.* from passport a
LEFT JOIN user b
ON a.id=b.passport_id

I can't see from your screenshot what the field names are for Consultant and Level hence the b.* to pickup all the fields from that table (I assume it IS on that table?). Anyway you can customize this query easily.

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