user962449
user962449

Reputation: 3873

adding an if statement to a mysql query

p1.username always has a value that's foreign to VB_user table. The problem is that p2.username has a default value of 0 and it's not a value in the VB_user table. So if I run the query it will only show the results if p2.username doesn't have a value of zero. How can I make it so if p2.username has the value of 0 it will show in my results, since VB_user.userid doesn't have a 0 can it, can I still show something?

In other words, I want to be able to show the results of p2.username even if its value is 0.

Here's my query:

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

Upvotes: 0

Views: 50

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65314

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

Upvotes: 1

Related Questions