来自1个表

时间:2016-10-28 09:30:38

标签: mysql sql

我有2个mysql表,如下所示:

表1:

ID      NAME
1       car1
2       car2
3       car3
4       car4

表格2:

car_id      liter
2           100
2           300
3           400
1           500
3           600

我想输出类似的东西:

表格3:

car_id      liters
car2        100 
car2        300
car3        400
car1        500
car3        600

我尝试写一些类似的东西:

SELECT tabel2.car_id
     , tabel1.ID 
  FROM tabel2
     , tabel2 
 INNER 
  JOIN tabel3  
    ON tabel2.car_id = tabel1.ID;

我知道这是一个新手问题,但我是SQL的新手。

4 个答案:

答案 0 :(得分:1)

只需加入两个表:

select t1.name, t2.liter
from table1 t1
join table2 t2 on t1.id = t2.car_id
order by t2.liter

答案 1 :(得分:0)

您可以尝试这样做以获得所需的结果:

select t1.NAME, t2.liter
from table1 t1
INNER JOIN table t2 on t1.id=t2.car_id
ORDER BY t2.liter DESC

答案 2 :(得分:0)

select t1.name, t2.liter from table1 t1 ,table2 t2 where t1.id = t2.car_id 
order by t2.liter;

答案 3 :(得分:-1)

试试这个:

select t1.name, t2.liter
  from table1 t1 ,
       table2 t2
 here t1.id = t2.car_id ;