I am trying to join 4 tables together, each with a column named Common Column
, and another column containing numbers. Thus, when joined a table with 5 columns should be created, one Common Column
column, and one columns from each of the 4 tables. However, the Common Column
is being created with each join, so it shows up three times in the resulting dataset.
Code:
SELECT *
FROM `Table 1`
INNER JOIN `Table 2`
ON `Table 1`.`Common Column` = `Table 2`.`Common Column`
INNER JOIN `Table 3`
ON `Table 3`.`Common Column` = `Table 2`.`Common Column`
INNER JOIN `Table 4`
ON `Table 4`.`Common Column` = `Table 3`.`Common Column`;
EDIT:
When I use
SELECT `Common Column`, num1, num2, num3, num4
as my first line, I get the following error:
Error Code: 1052. Column `Common Column` in field list is ambiguous
If you join using the USING syntax, the common column will only appear once:
SELECT *
FROM `Table 1`
INNER JOIN `Table 2` USING (`Common Column`)
INNER JOIN `Table 3` USING (`Common Column`)
INNER JOIN `Table 4` USING (`Common Column`);
That said, do avoid using select *
; it is always better to explicitly list the columns you want (qualifying them with table if needed). If you do
SELECT `Table 1`.`Common Column`, num1, num2, num3, num4
then it doesn't matter how you join.
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.