Evgenij Reznik
Evgenij Reznik

Reputation: 18594

Conditionally execute query

Is there a way to execute a query, depending on the result of a certain column?

My table has a column col1, which could be NULL. I want to check first. If col1 is not NULL, execute another query, if col1 is NULL, do nothing (or return something else).

In pseudocode it could look like this:

IF (SELECT col1 IS NUT NULL FROM `tab1`)
THEN (SELECT col1, col2, col3 FROM `tab1`)

PS: I execute those queries from PHP, so it would also be possible, to check the result of col1 with PHP, though I would prefer to use plain SQL.

Upvotes: 0

Views: 70

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

the where clause is your if

  select col1, col2, col3 FROM `tab1`
  where col1 IS NOT NULL

Upvotes: 1

Related Questions