如何留下外連接2表?grails hql左外連接
class Person {
String firstName
String lastName
String gender
//static hasMany = [votes: Vote]
static mapping = {
version false
}
static constrains = {
}
}
class Vote {
Person voter;
Person subject;
static mapping = {
version false
}
static constraints = {
voter nullable: false
subject nullable: false
}
}
我需要讓每個人都沒有受到投票,對於一個特定的人。 可以說5人中有3人得到1票,我需要另外2人,他沒有投票給他看。 查詢應該是怎樣的?
編輯:
def personInstance1 = new Person(firstName: "Antonio", lastName: "Vivaldi", gender: "m")
def personInstance2 = new Person(firstName: "Dennis", lastName: "Rodman", gender: "m")
def personInstance3 = new Person(firstName: "Marc", lastName: "Oh", gender: "m")
def personInstance4 = new Person(firstName: "Gudrun", lastName: "Graublume", gender: "w")
def personInstance5 = new Person(firstName: "Hilde", lastName: "Feuerhorn", gender: "w")
def personInstance6 = new Person(firstName: "Mandy", lastName: "Muller", gender: "w")
personInstance1.save()
personInstance2.save()
personInstance3.save()
personInstance4.save()
personInstance5.save()
personInstance6.save()
def voteInstance1 = new Vote(voter: personInstance1, subject: personInstance2)
def voteInstance2 = new Vote(voter: personInstance1, subject: personInstance3)
def voteInstance3 = new Vote(voter: personInstance1, subject: personInstance4)
def voteInstance4 = new Vote(voter: personInstance1, subject: personInstance5)
def voteInstance5 = new Vote(voter: personInstance2, subject: personInstance1)
voteInstance1.save()
voteInstance2.save()
voteInstance3.save()
voteInstance4.save()
voteInstance5.save()
這是我的Grails引導文件,安東尼和丹尼斯已經投票,每個需要提交的他們沒有投票的人的名單。
編輯: 這樣我似乎得到了丹尼斯的結果,因爲他只投一次,但 如果我把v.voter_id = 1, 得到了安東尼一個結果的,按一倍他做了多少票。
SELECT first_name FROM vote as v
LEFT OUTER JOIN person as p
ON v.subject_id != p.id AND v.voter_id = 2
WHERE p.id IS NOT NULL
你想知道穆勒沒有被維瓦爾第等人投票嗎? – 2014-10-16 15:02:33
是的:)穆勒應該返回維瓦爾第作爲投票的候選人... 和rodman應該得到返回除vivaldi每個人。 – goran 2014-10-16 15:12:12
包括我用sql得到了多少,但仍然hql語法也將讚賞,如標題 – goran 2014-10-17 09:37:36