473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

if condition UPDATE else SET to zero

code green
1,726 Expert 1GB
I have two tables.
`tempTable` has two fields `part` and `qty`
`products` also has `part` and `qty` fields and a `last_updated` date stamp.
I wish to UPDATE `products`.`qty` with `tempTable.`qty` and date stamp it
BUT only if the `qty` fields differ.
Straightforward enough
Expand|Select|Wrap|Line Numbers
  1. UPDATE `products` JOIN `tempTable` USING(`part`)
  2. SET `products`.`qty` = `tempTable`.`qty`,
  3. `products`.last_updated` = NOW()
  4. WHERE `products`.`qty` != `tempTable`.`qty`
But if the part exists in products but not in tempTable I wish to set `products`.`qty` to zero.

Can this be done in one query?
I am sure it can with some combination of IF ELSE / IF EXISTS but I am struggling (just back of holiday)
Two queries is fine but I would prefer one.
Any help appreciated
Aug 14 '07 #1
2 3757
pbmods
5,821 Expert 4TB
Heya, Code Green.

Something like this, perhaps:

Expand|Select|Wrap|Line Numbers
  1. UPDATE
  2.     (
  3.             `products`
  4.         JOIN
  5.             `tempTable`
  6.                 USING
  7.                     (`part`)
  8.     )
  9.     SET
  10.         `products`.`qty` = `tempTable`.`qty`,
  11.         `products`.last_updated` = NOW(),
  12.         `products`.`qty` = IF
  13.         (
  14.             EXISTS
  15.             (
  16.                 SELECT
  17.                         *
  18.                     FROM
  19.                         `tempTable`
  20.                     WHERE
  21.                         `part` = `products`.`part`
  22.             ),
  23.             `products`.`qty`,
  24.  
  25.         )
  26.     WHERE
  27.         `products`.`qty` != `tempTable`.`qty`
  28.  
Not sure if this will work, and at any rate, it's probably no faster than doing two queries anyway.
Aug 14 '07 #2
code green
1,726 Expert 1GB
Thanks pbmods. That looks quite impressive.
I'll give it a try but like you suspect, sub-queries are very slow.
Aug 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: J.P | last post by:
Hi! Anyone knows if it's possible to do an update if, and only if my condition is TRUE. Example: In MYTABLE I have three columns, like this: ID(INT), PUBLISH(ENUM(Y,N)),...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
3
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a way that could block the compilation if the...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched multiple rows to be updated based on the ON condition...
0
by: rleroux | last post by:
Hi all, I have the following snippet of code in my trigger SELECT @Currentqty = qtyonHand FROM tblProduct WHERE ProductID = @Product SELECT @CurrentReorder = ReorderPoint FROM tblProduct WHERE...
8
by: voger | last post by:
Hi everyone Can anyone please explain this strange behavior? I have this function that is supposed to copy the contents of one array to one other array, but sorted. I don't know if the logic...
2
by: saturn99 | last post by:
I have a requirement to write the SQL code as follows(peseudo code): IF THEN INSERT INTO .... ELSE UPDATE .... END IF The codes must be executed as...
3
by: billa856 | last post by:
Hi, My project is in MS Access 2002.Its on Inventory In which I have one form, which I used for Shipping Data entry. I am using this code Private Sub Enter_Click() On Error GoTo...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.