egemen
egemen

Reputation: 839

select all rows if condition parameter is null

All rows

SELECT * FROM table

One row

SELECT * FROM table WHERE id = 3

I want to get all rows, how can i get ?

example

SELECT * FROM table WHERE id = 0

Upvotes: 0

Views: 4128

Answers (3)

Nisse Engström
Nisse Engström

Reputation: 4752

Possibly:

$query = 'SELECT * FROM table';

if (isset ($id)) {
  $query .= " WHERE id = $id";
}

Upvotes: 1

Thorsten Kettner
Thorsten Kettner

Reputation: 94884

You question is a bit unclear, but I guess you are looking for a way to select all ids in case the given id parameter is zero.

SELECT * FROM mytable WHERE id = @id OR @id = 0;

Or with NULL instead of zero:

SELECT * FROM mytable WHERE id = @id OR @id IS NULL;

Upvotes: 1

fmgonzalez
fmgonzalez

Reputation: 823

If you want to get all rows with parameter 0 then use this:

SELECT * FROM table WHERE id = ? OR ? = 0;

I hope that's what you want.

Upvotes: 2

Related Questions