SQLite中的文本引用和int引用之间的区别?

时间:2013-03-07 16:07:59

标签: sql database sqlite foreign-keys

以下声明有效:

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"("")People.specialty_area_1被标记为TEXT REFERENCES "SpecialtyAreas._id"("").

数据类型之间的差异是否会导致此问题?

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

是否有更简单的方法来查询数据库? specialty_areas查询完成了我想要的内容;我只是想弄清楚为什么我无法从office查询得到相同的结果。

1 个答案:

答案 0 :(得分: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