0

我会尽量解释这个问题。我有两个表“users1213”,这是一个玩家表和“Loanperiodes”。现在我正在尝试使用 sql-query 立即获得目前在俱乐部的球员列表。如果他们被租借到另一家具乐部,他们不必在名单上。俱乐部也有可能租借一名球员,所以这些球员也必须在名单上。所以我现在有这个(俱乐部有一个id“1”,球队也有一个id“1”):

SELECT users1213.id, surname, name, team, club, loanclub
FROM users1213
LEFT JOIN loanperiode ON users1213.id = loanperiode.player
WHERE users1213.status =  'player'
AND (
(
users1213.club !=  '1'
AND loanperiode.loanclub =  '1'
AND loanperiode.begin <=  '2013-02-03'
AND loanperiode.end >=  '2013-02-03'
AND (
uitleenperiodes.team_id =  '1'
)
)
OR (
users1213.club =  '1'
AND users1213.team =  '1'
AND (
loanperiode.loanclub IS NULL 
OR (
loanperiode.loanclub IS NOT NULL 
AND (
loanperiode.begin <  '2013-02-03'
AND loanperiode.end <  '2013-02-03'
)
OR (
uitleenperiodes.begin >  '2013-02-03'
AND uitleenperiodes.end >  '2013-02-03'
)
)
)
)
)
GROUP BY users1213.id
ORDER BY name
LIMIT 0 , 30

通过这个查询,我得到了一个很好的结果,但有 1 个问题,当有人获得 2 个贷款期时。例如,PLAYER1 现在被租借给 CLUB A,因此他可能不会被列出。但是当 PLAYER1 也有一个已经过期的租借期时,到 CLUB B,PLAYER1 会因为 LEFT JOIN 而被列出。有没有解决方案可以通过查询解决这个问题,或者我必须在运行阵列时进行检查?

非常感谢!

4

1 回答 1

0
SELECT users1213.id, surname, name, team, club, loanclub
FROM users1213
LEFT JOIN loanperiode ON users1213.id = loanperiode.player
    AND '2013-02-03' BETWEEN loanperiode.begin AND loanperiode.end

WHERE
loanperiode.loanclub =  '1'
OR (loanperiode.loanclub IS NULL -- I suppose this happens only when there is no loan between those dates so nothing LEFT JOINed
AND users1213.club = '1')

ORDER BY name

这个例子假设用户一次只能拥有一笔贷款(是的,他可以拥有更多在历史上结束的贷款,但只有一笔在今天之前开始并在未来结束)。

于 2013-02-03T23:57:41.577 回答