假設我們在MySQL數據庫中具有以下數據結構和數據中的Person,Character和PersonCharacter類及其相應的結構。兩個表的SQL和/或條件
table persons
[
{ id: 1, name: peter },
{ id: 2, name: mary },
{ id: 3, name: joe },
{ id: 4, name: paul },
{ id: 5, name: may },
]
table characters
[
{ id: 101, description: cheerful },
{ id: 102, description: nervous },
{ id: 103, description: greedy },
{ id: 104, description: sincere },
{ id: 105, description: naive },
{ id: 106, description: firm },
]
table person_characters
[
{id: 1000, person_id: 1, character_id: 101},
{id: 1001, person_id: 1, character_id: 103},
{id: 1002, person_id: 1, character_id: 106},
{id: 1003, person_id: 2, character_id: 102},
{id: 1004, person_id: 2, character_id: 104},
{id: 1005, person_id: 3, character_id: 102},
{id: 1006, person_id: 3, character_id: 105},
{id: 1007, person_id: 4, character_id: 104},
{id: 1008, person_id: 5, character_id: 101},
{id: 1009, person_id: 5, character_id: 106},
]
如何在可以藉助Rails的幫助下編寫SQL語句,可以找出具有特定字符的人?例如,性格既堅定又快樂的人可能會和彼得。
這個問題似乎很典型,但很難通過搜索引擎找到答案。
你已經知道了「人物」身份證嗎?你需要看看嗎? [這本質上是一個JOIN查詢,但如果你還沒有它,需要更深層次的交叉引用字符標識。] – 2011-01-10 05:20:39