[英]Join three tables with primary key in one and foreign key in other two tables
enter image description here在此处输入图片说明
$query="SELECT i.trans_date, f.col_code,f.trans_qty ,t.dept_code
FROM table1 AS i
LEFT JOIN table2 AS f
ON f.trans_no=i.trans_no
LEFT JOIN table3 AS t
ON t.trans_no=i.trans_no";
trans_no --> Primary key in table 1
trans_no --> Foreign key in table 2,3
I am trying to fetch the above fields from the 3 tables but not getting it?我试图从 3 个表中获取上述字段但没有得到它?
I wanna fetch date(tbl1),qty(tbl2),col_code(tbl2),col_code(tbl3),dept_code(tbl3)
.. Plz Help我想获取date(tbl1),qty(tbl2),col_code(tbl2),col_code(tbl3),dept_code(tbl3)
.. 请帮助
Probably doesn't have the same key values.可能没有相同的键值。 Please check the row data in your tables.请检查表中的行数据。
You should use alias for columns col_code for avoid ambiguity ( and related query error ) due the fact these column name is present in tow different tables involved in the same query eg you could add the alias col_code_t2 and cold_code_t3您应该为列 col_code 使用别名以避免歧义(和相关查询错误),因为这些列名存在于同一查询中涉及的两个不同表中,例如您可以添加别名 col_code_t2 和 Cold_code_t3
query="Select i.trans_date
, f.col_code col_code_t2
, f.trans_qty
, t.col_code cold_code_t3
, t.dept_code
from table1 AS i
LEFT JOIN table2 AS f ON f.trans_no=i.trans_no
LEFT JOIN table3 AS t ON t.trans_no=i.trans_no";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.