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 r开发者_如何学编程eturn true. However, if current
meets or exceeds capacity
, I would like the query to return false.
Thanks!
in mysql that's easy:
select (current >= capacity) as exceeded from table
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
}
精彩评论