Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两张桌子 1. Artist, 2. Photo而且有one to many关系。
1. Artist
2. Photo
one to many
我想找到all artists那个有no photos和less then 5 photos。
all artists
no photos
less then 5 photos
我该如何为此编写查询?
试试这个。
Artist.join(:photos).having("count(photos.*)<5")
你可以找到那些使用这个
Artist.join(:photos).group("photos.artist_id").select("sum(photos.artist_id) as total_photos").having("total_photos<5")