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
  • 681
  • 1
  • 8
  • 25

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
  • 27,326
  • 10
  • 44
  • 54
dustytrash
  • 1,518
  • 1
  • 9
  • 15
2

Use IFNULL function:

SELECT IFNULL(SELECT id 
              FROM project_team 
              WHERE name = ? 
                AND project_id = ?, 0)
Madhur Bhaiya
  • 27,326
  • 10
  • 44
  • 54