0

我有 2 张桌子:

Appointments
id serviceId
1  1
2  1

Services
id
1

约会表通过使用 serviceId 列链接到服务表。从约会表中检索数据时,我还想从服务表中检索数据,但每个表中的列数不匹配。

我试过这个:

SELECT appointments.*, services.* FROM appointments
INNER JOIN services ON id = appointments.serviceId
WHERE id = ?

但这不起作用。首先,在连接中,如何从约会表中引用一个我什至还没有检索到数据的 ID?其次,如果两个列名匹配,如何从两个表中获取所有数据,然后检索数据?

这不起作用:

results.getInt("id");

因为两个表都有一个 id 字段。

4

1 回答 1

1

指定ID所属的表名,

SELECT appointments.*, services.* 
FROM appointments
     INNER JOIN services 
         ON Services.id = appointments.serviceId
-- WHERE id = ? -- specify also here as the two tables have column ID

ALIAS在它们周围添加

SELECT  appointments.id as AppID,
        appointments.serviceId as AppServiceID
        services.id AS ServiceID
FROM    appointments
        INNER JOIN services
            ON Services.id = appointments.serviceId
-- WHERE   id = ?

当你得到他们的值时,现在使用他们的别名,像这样,

results.getInt("AppID");
于 2012-10-07T04:33:49.490 回答