0

以下语句有效:

SELECT p._id, last_name, first_name_pref, title, photo_big
FROM People p NATURAL JOIN SpecialtyAreas s
WHERE specialty_area_1 = (SELECT s._id FROM SpecialtyAreas s WHERE s.name = 'Customer Service')
ORDER BY last_name, first_name_pref;

然而,这个没有:

SELECT p._id, last_name, first_name_pref, title, photo_big
FROM People p NATURAL JOIN Offices o
WHERE office = (SELECT o._id FROM Offices o WHERE o.name = 'Beadle County')
ORDER BY p.last_name, p.first_name_pref;

我可以在两个表之间找到的唯一区别People.office是标记为INTEGER REFERENCES "Offices._id"("")whilePeople.specialty_area_1标记为TEXT REFERENCES "SpecialtyAreas._id"("").

数据类型之间的差异会导致这个问题吗?

此外,这两个字符串实际上都在数据库中。

有没有更简单的方法来查询数据库?specialty_areas查询完成了我想要的;我只是想找出为什么我不能从office查询中得到相同的结果。

4

1 回答 1

0

您希望数据类型在外键关系中涉及的父表列和子表列上匹配。

旁观者通常认为简单,但我喜欢内联视图,因为它们有点自我记录:

       select * from people p
       join
       (
        select _id from people join offices
        on people.office = offices._id 
        and offices.name = 'Beadle County'

        UNION

        select _id from people join specialtyareas
        on people.specialty_area_1 = specialtyareas._id 
        and specialtyareas.name='Customer Service'

       ) as MatchingPeople
       on MatchingPeople._id = p._id
于 2013-03-07T17:45:26.130 回答