我有以下兩個表:有麻煩了PostgreSQL的查詢
用戶
userid | email | phone
1 | [email protected] | 555-555-5555
2 | [email protected] | 555-444-3333
3 | [email protected] | 333-444-1111
4 | [email protected] | 123-333-2123
UserTag
id | user_id | tag
1 | 1 | tag1
2 | 1 | tag2
3 | 1 | cool_tag
4 | 1 | some_tag
5 | 2 | new_tag
6 | 2 | foo
6 | 4 | tag1
我想運行在SQLAlchemy的查詢加入這兩個表並返回沒有標籤「tag1」或「tag2」的所有用戶。在這種情況下,查詢應返回用戶2和3。任何幫助將不勝感激。
我需要這個查詢的反面:
users.join(UserTag, User.userid == UserTag.user_id)
.filter(
or_(
UserTag.tag.like('tag1'),
UserTag.tag.like('tag2')
))
)
我一直在這個持續了幾個小時,但總是用錯誤的用戶或有時他們都結束了。實現這一點的SQL查詢也會有所幫助。我會嘗試將其轉換爲SQLAlchemy。