简体   繁体   English

如何编写 SQL 查询以从多个表中获取数据?

[英]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我需要从consulter.table中选择所有顾问信息

first_name & last_name & email & phone from passport.table来自passport.table的first_namelast_name以及emailphone

在此处输入图片说明

I have passport_id in users table我在用户表中有passport_id

在此处输入图片说明

Number of consultant and his level from main table of query consultant.table查询consultant.table主表中的顾问Number of consultantlevel

Use this sql query to get your result:使用此 sql 查询获取结果:

  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检查此 SQL Fiddle 的输出

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?).我无法从您的屏幕截图中看到顾问和级别的字段名称是什么,因此b.*可以从该表中提取所有字段(我假设它在该表上?)。 Anyway you can customize this query easily.无论如何,您可以轻松自定义此查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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