2014-10-16 81 views
2

如何留下外連接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 
+0

你想知道穆勒沒有被維瓦爾第等人投票嗎? – 2014-10-16 15:02:33

+0

是的:)穆勒應該返回維瓦爾第作爲投票的候選人... 和rodman應該得到返回除vivaldi每個人。 – goran 2014-10-16 15:12:12

+0

包括我用sql得到了多少,但仍然hql語法也將讚賞,如標題 – goran 2014-10-17 09:37:36

回答

1

試試這個:

SELECT * FROM Person P 
WHERE NOT EXISTS(
    SELECT 'Vote' FROM Vote V 
    WHERE V.subject = P 
) 

這樣你會提取所有的人,但無表決權

編輯

在SQL你可以獲取這樣一個矩陣:

CREATE TABLE #person (nome varchar(30)) 
CREATE TABLE #vote (votante varchar(30), candidato varchar(30)) 

INSERT INTO #person values 
('Antonio Vivaldi'), 
('Dennis Rodman'), 
('Marc Oh'), 
('Gudrun Graublume'), 
('Hilde Feuerhorn'), 
('Mandy Muller') 

INSERT INTO #vote values 
('Antonio Vivaldi', 'Dennis Rodman'), 
('Antonio Vivaldi', 'Marc Oh'), 
('Antonio Vivaldi', 'Gudrun Graublume'), 
('Antonio Vivaldi', 'Hilde Feuerhorn'), 
('Dennis Rodman', 'Antonio Vivaldi') 

SELECT * 
FROM #person p 
CROSS JOIN #person c 
WHERE NOT EXISTS(
    SELECT 'X' 
    FROM #vote v 
    WHERE v.votante = p.nome 
    AND v.candidato = c.nome 
) 
AND p.nome <> c.nome 
ORDER BY p.nome 
+0

謝謝,但這種方式我只得到所有人沒有主題ID。 我其實需要一個特定的人。例如,person1對5人中的2人進行投票,對於5人中的1人,投票人將得到2票,所以每個人的結果必須不同。 – goran 2014-10-16 12:36:14

+0

@goran:我在哪裏找到候選人名單?因爲如果您已經爲該候選人投了票,我認爲存在Vote對象,但是如果我不對候選人投票,我找不到它。如果你有一個候選人名單,我們可以開始在那裏 – 2014-10-16 13:16:40

+0

已更新的帖子與一些人和投票實例 – goran 2014-10-16 13:31:02