如何选择三个具有相同列名但值不同的表

时间:2018-09-24 05:57:10

标签: mysql

嗨,我很困惑,要选择3个表数据值。

例如: 在表1中:

  LocationID, Start, SetRowNum, LocRowNum, Difference

     GAP

      3        600       7           1          6      (3,6)

     GAP

在表2中

(locationID, IslandID)

在表3

CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its 
         dependencies could not be resolved: The following artifacts could not be resolved: org.apache.maven:maven-plugin-api:jar:2.0.9, org.apache.maven:maven-artifact:jar:2.0.9, org.codehaus.plexus:plexus-utils:jar:
         1.5.1, org.apache.maven:maven-core:jar:2.0.9, org.apache.maven:maven-settings:jar:2.0.9, org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.9, org.apache.maven:maven-profile:jar:2.0.9, 
         org.apache.maven:maven-model:jar:2.0.9, org.apache.maven:maven-repository-metadata:jar:2.0.9, org.apache.maven:maven-error-diagnostics:jar:2.0.9, org.apache.maven:maven-project:jar:2.0.9, 
         org.apache.maven:maven-plugin-registry:jar:2.0.9, org.apache.maven:maven-plugin-descriptor:jar:2.0.9, org.apache.maven:maven-artifact-manager:jar:2.0.9, org.apache.maven:maven-monitor:jar:2.0.9, 
         org.apache.maven:maven-toolchain:jar:1.0, org.apache.maven.shared:maven-shared-utils:jar:0.1, org.apache.maven.shared:maven-shared-incremental:jar:1.1, org.codehaus.plexus:plexus-compiler-api:jar:2.2, 
         org.codehaus.plexus:plexus-compiler-manager:jar:2.2, org.codehaus.plexus:plexus-compiler-javac:jar:2.2, org.codehaus.plexus:plexus-container-default:jar:1.5.5: Failure to transfer org.apache.maven:maven-
         plugin-api:jar:2.0.9 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. 
         Original error: Could not transfer artifact org.apache.maven:maven-plugin-api:jar:2.0.9 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.
        - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (execution: default-testCompile, phase: test-compile)
        - CoreException: Could not get the value for parameter compilerId for plugin execution default-testCompile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one 
         of its dependencies could not be resolved: The following artifacts could not be resolved: org.apache.maven:maven-plugin-api:jar:2.0.9, org.apache.maven:maven-artifact:jar:2.0.9, org.codehaus.plexus:plexus-
         utils:jar:1.5.1, org.apache.maven:maven-core:jar:2.0.9, org.apache.maven:maven-settings:jar:2.0.9, org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.9, org.apache.maven:maven-profile:jar:2.0.9, 
         org.apache.maven:maven-model:jar:2.0.9, org.apache.maven:maven-repository-metadata:jar:2.0.9, org.apache.maven:maven-error-diagnostics:jar:2.0.9, org.apache.maven:maven-project:jar:2.0.9, 
         org.apache.maven:maven-plugin-registry:jar:2.0.9, org.apache.maven:maven-plugin-descriptor:jar:2.0.9, org.apache.maven:maven-artifact-manager:jar:2.0.9, org.apache.maven:maven-monitor:jar:2.0.9, 
         org.apache.maven:maven-toolchain:jar:1.0, org.apache.maven.shared:maven-shared-utils:jar:0.1, org.apache.maven.shared:maven-shared-incremental:jar:1.1, org.codehaus.plexus:plexus-compiler-api:jar:2.2, 
         org.codehaus.plexus:plexus-compiler-manager:jar:2.2, org.codehaus.plexus:plexus-compiler-javac:jar:2.2, org.codehaus.plexus:plexus-container-default:jar:1.5.5: Failure to transfer org.apache.maven:maven-
         plugin-api:jar:2.0.9 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. 
         Original error: Could not transfer artifact org.apache.maven:maven-plugin-api:jar:2.0.9 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.
        - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (execution: default-compile, phase: compile)

在这种情况下,我如何获取所有具有ID的价格值

我需要类似

的结果
Id    value   price  <br> 
1     101     30   <br> 
2     102     40  <br>
3     103     50  <br>

4 个答案:

答案 0 :(得分:1)

尝试使用内部联接

select id,table1.value, table1.price as price1,table2.price as price2,table3.price as price3 from table1 
inner join table2 on table1.value=table2.value
inner join table3 on table2.value=table3.value

答案 1 :(得分:0)

SELECT table1.id , table1.price , table2.price ,table3.price 
FROM table1 
JOIN table2 ON table.id = table2.id 
JOIN table3 ON table.value = table3.value

结果

答案 2 :(得分:0)

select table1.id,table1.value, table1.price,table2.price,table3.price
来自表1的

table1.value = table2.value上的内部联接table2 table2.value = table3.value上的内部联接table3

答案 3 :(得分:0)

您需要做的是使用称为 primary foreign 键的连贯值在表上执行JOIN。在这种情况下,将表链接在一起的连贯值是名为 value 的列。这就是您在JOIN

中需要使用的内容
SELECT
    table1.id as TABLE1_ID,
    table1.price as TABLE1_PRICE,
    table2.price as TABLE2_PRICE,
    table3.price as TABLE3_PRICE
FROM
  table1
LEFT JOIN
    table2 ON table1.`value` = table2.`value`
LEFT JOIN
    table3 ON table2.`value` = table3.`value`

输出:

enter image description here

Working SQL fiddle

有关JOIN的{​​{3}}的更多信息。

由于您在注释部分提到了PHP,因此请尝试以下操作:

$sql="SELECT
        table1.id as TABLE1_ID,
        table1.price as TABLE1_PRICE,
        table2.price as TABLE2_PRICE,
        table3.price as TABLE3_PRICE
    FROM
      table1
    LEFT JOIN
        table2 ON table1.`value` = table2.`value`
    LEFT JOIN
        table3 ON table2.`value` = table3.`value`"
$result_set=mysqli_query($conn, $sql);
$row=mysqli_fetch_array($conn, $result_set);

$rowcount=mysqli_num_rows($result_set);
$count=0;

while($rowcount > $count) {
    $count++;

    echo 'id: '.$row['id'].'<br />';
    echo 'price: '.$row['price'].'<br />';

    $row=mysqli_fetch_array($conn, $result_set);
}

请记住include您的数据库连接文件,以便可以访问$conn

如果最初不起作用,请尝试用定义的别名换出实际的列名,如下所示:

$row['id']$row['TABLE1_ID']

相关问题