父子关系,不使用PHP获取并重新使用返回值


Parent-child relation, without using PHP fetching and re-using the returning value

我正在尝试建立两个表之间的关系。我可以用PHP获取数据,并在查询中再次使用返回值,从而完成我想要的操作。但就我所研究的而言,这似乎不是一个有效的方法。

当我用SQL查询将赌注#3确定为YES时,我如何获得中奖优惠券?

(应返回3和4)

优惠券表,

ID coupon_id bets bets_played  played_by
0    2        2       yes         JOHN
1    2        3       no          JOHN
2    3        1       yes         JANE
3    3        3       yes         JANE
4    4        3       yes         SARAH
5    4        2       no          SARAH

投注表,

ID  result
1    yes
2    no
3    NULL

谢谢。

select distinct c.*
from coupon c
join bets b on b.result = c.bets_played and b.result = 'yes'