Home > Mobile >  (SQL) Select all if ID = 0
(SQL) Select all if ID = 0

Time:10-21

im still very unexperienced with SQL, so this might be a stupid question.

Let's say I have a Statement something like this:

SELECT * WHERE table_menu.year = @year
                            AND table_menu.user = @user
                            AND table_menu.history = 'false'

@user would be a Number from 0 - 100 how would I manage to get a table with all the Table contents if the user was 0?

CodePudding user response:

You could either do that on client-side, so where you set the sql-parameter. If you know that user is 0 which should be treated as "all users", then simply select all from table_menu:

SELECT * FROM table_menu
WHERE table_menu.year = @year
AND table_menu.history = 'false'

or you could do that in the database, maybe with a performance impact:

SELECT * FROM table_menu 
WHERE table_menu.year = @year
AND (@user = 0 OR table_menu.user = @user)
AND table_menu.history = 'false'
  •  Tags:  
  • sql
  • Related