tau
tau

Reputation: 6749

MySQL condition

I'm still pretty new with MySQL and I don't know the best way to do this.

I have a table with incremented values, and if the values exceed a certain limit, I'd like to know. So lets say I have this table with a current column and capacity column. A row in current is incremented by user input and I want to know when the value in a current row exceeds or meets its corresponding capacity.

If current is still below the capacity, I would like the query to return true. However, if current meets or exceeds capacity, I would like the query to return false.

Thanks!

Upvotes: 2

Views: 205

Answers (2)

BoltClock
BoltClock

Reputation: 724462

Add a WHERE clause to do that check in your UPDATE query:

// I assume you mean you want to increment the current column by 1,
// but it's your table so change the SET values as needed
$updated = mysql_query('UPDATE table SET current = current + 1 WHERE id = ' . intval($row_id) . ' AND current < capacity');

Then check mysql_affected_rows() of the UPDATE query:

if (mysql_affected_rows() > 0)
{
    // Success
}

Upvotes: 2

knittl
knittl

Reputation: 265908

in mysql that's easy:

select (current >= capacity) as exceeded from table

Upvotes: 4

Related Questions