在mysql查询中添加if语句

时间:2012-01-05 01:16:26

标签: mysql join

p1.username始终具有VB_user表的外来值。问题是p2.username的默认值为0,而且它不是VB_user表中的值。因此,如果我运行查询,它将仅显示p2.username的值不为零的结果。如果p2.username的价值0会显示在我的搜索结果中,我怎么能这样做呢,因为VB_user.userid没有0可以,我还能吗什么东西?

换句话说,我希望能够显示p2.username的结果,即使其值为0

这是我的疑问:

SELECT p.r1, p.r2, p.rounds, p.whenmatch, p.created, p.status, p.lid, p1.userid AS userid1, p1.username AS challenger, p2.userid AS userid2, p2.username AS challenged, p3.lname 
        FROM AB_league_match p 
        JOIN VB_user p1 on p.challenger = p1.userid 
        JOIN VB_user p2 on p.challenged = p2.userid 
        JOIN AB_league p3 on p.lid = p3.id

1 个答案:

答案 0 :(得分:1)

SELECT p.r1, p.r2, p.rounds, p.whenmatch, p.created, p.status, p.lid, p1.userid AS userid1, p1.username AS challenger, p2.userid AS userid2, p2.username AS challenged, p3.lname 
        FROM AB_league_match p 
        JOIN VB_user p1 on p.challenger = p1.userid 
        LEFT JOIN VB_user p2 on p.challenged = p2.userid 
        JOIN AB_league p3 on p.lid = p3.id