SQLite3 INNER JOIN有3个表

时间:2014-01-29 19:13:32

标签: sql sqlite inner-join

我正在尝试从3个表中选择INNER JOIN:

表格:

CREATE TABLE tracks (
'track_id' INTEGER PRIMARY KEY NOT NULL,
'name' TEXT NOT NULL,
'length' REAL DEFAULT '0.00',
'city' TEXT
);

CREATE TABLE heats (
'heat_id' INTEGER PRIMARY KEY NOT NULL,
'track_id' INTEGER UNSIGNED NOT NULL,
'heat_pos' INTEGER UNSIGNED NOT NULL,
'day_pos' INTEGER UNSIGNED NOT NULL,
'type' TEXT NOT NULL DEFAULT 'training',
'average' REAL,
'date' TEXT,
'comment' TEXT,
FOREIGN KEY ('track_id') REFERENCES tracks ('track_id')
);

CREATE TABLE laps (
'lap_id' INTEGER PRIMARY KEY NOT NULL,
'heat_id' INTEGER UNSIGNED NOT NULL,
'laptime' REAL UNSIGNED NOT NULL,
FOREIGN KEY ('heat_id') REFERENCES heats ('heat_id')
);

从2张桌子(圈数和热量)中选择信息时,它的效果与我预期的相同:

select 
laps.lap_id, 
laps.laptime,
heats.heat_pos
from laps
inner join heats on laps.heat_id = heats.heat_id;

但现在我想从曲目表中选择相应的曲目名称:

select 
laps.lap_id, 
laps.laptime,
heats.heat_pos,
tracks.name
from laps, tracks, heats
inner join heats on laps.heat_id = heats.heat_id and
inner join heats on tracks.track_id = heats.track_id;

这给了我以下错误:

ambiguous column name: heats.heat_pos

我完全迷失了,但我感觉这只是一个小错误。 谁知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

select 
laps.lap_id, 
laps.laptime,
heats.heat_pos,
tracks.name
from laps
inner join heats on laps.heat_id = heats.heat_id 
inner join tracks on tracks.track_id = heats.track_id;

答案 1 :(得分:-1)

select 
laps.lap_id, 
laps.laptime,
heats.heat_pos,
tracks.name
from laps
inner join heats on laps.heat_id = heats.heat_id
inner join heats on tracks.track_id = heats.track_id
ORDER BY laps.lap_id