0

I have a simple query here, basically I want to check the rows for a combination of parameters to have each person logged on a project only once. I want to check for the id/info combo and return either the id or a 0 for new entry. The query here returns nil, but I'd like to return 0 if no results found. I can check all rows using case or IF statements, but I just can't get the query right to select based on the results from all rows. Thanks!

SELECT id FROM project_team WHERE name = ? AND project_id = ?
nick
  • 789
  • 1
  • 11
  • 27

2 Answers2

2

You could use the IFNULL() function:

SELECT IFNULL(id, '0') FROM project_team WHERE name = ? AND project_id = ?

Or possibly:

SELECT(IFNULL((SELECT id FROM project_team WHERE name = ? AND project_id = ?), 0))
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
dustytrash
  • 1,568
  • 1
  • 10
  • 17
2

Use IFNULL function:

SELECT IFNULL(SELECT id 
              FROM project_team 
              WHERE name = ? 
                AND project_id = ?, 0)
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57