Using the conditions for query UPDATE

A simple trick to update multiple records in the table with a single query is that the conditions of Use, do an example: we have a database of products for which we wish to change the discount on certain types of goods by establishing different amounts for each category.

The instruction to use may be similar to the following:

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">UPDATE prodotti</span> UPDATE products</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">SET scontistica =</span> September discounts =</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">CASE</span> CASE</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">WHEN prodotto = 'stampanti' THEN 10</span> WHEN product = 'printers' THEN 10</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">WHEN prodotto = 'pen drive' THEN 7</span> WHEN product = 'pen drive' THEN 7</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">WHEN prodotto = 'hard disk' THEN 4</span> WHEN product = 'disk' THEN 4</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">ELSE scontistica</span> ELSE discounts</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">END</span> END</span>

<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">WHERE Id IN (3,5,6,9);</span> WHERE Id IN (3,5,6,9);</span>

Thanks to the WHERE clause and the unique identifiers in this table, the search will only affect the values of the products for which you want to alter the discounts, the value for other products will be rather unaffected by the condition introduced by CASE.

This entry was posted in Database and tagged , , . Bookmark the permalink.

Comments are closed.