|
|
Subject:
Update field with incrementing number in some rows. (MySQL)
Category: Computers > Programming Asked by: qvtqht-ga List Price: $10.00 |
Posted:
02 Apr 2004 17:50 PST
Expires: 02 May 2004 18:50 PDT Question ID: 324313 |
I would like to reproduce the pseudo-code below in one MySQL query. (I am using the ezSQL class for the $db object.) The code selects all approved customers from the customers table and then updates their priority_id to a consecutive incrementor. This is performed only for approved customers, while other customers' priority_id is set to NULL. The query would look something like this: UPDATE customers SET priority_id = ++i WHERE approved = 1; Note, that I am not looking for an alternative solution to my "problem". I am looking for an answer to the specific request stated above. -- Here is the pseudo-code: $db->query("UPDATE customers SET priority_id = NULL WHERE approved != 1 ORDER BY date_added"); $customers = $db->get_results("SELECT customer_id, priority_id FROM customers WHERE approved = 1"); if (count($customers) > 0) { foreach($customers as $customer) { $i++; if ($customer->priority_id != $i) $db->query("UPDATE customers SET priority_id = $i WHERE customer_id = $customer->customer_id"); } } | |
| |
|
|
There is no answer at this time. |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |