Solved

MySQL UPDATE SET query

Posted on 2013-01-05
1
586 Views
Last Modified: 2013-01-10
# I have the following table:

CREATE TABLE RES (S int, R int, Q int, P int, RES varchar(100), M int );

# With the following contents:

INSERT INTO RES (S,R,Q,P,RES,M) VALUES
(1,1,1,20,"London",1),
(1,1,1,20,"Paris",0),
(1,1,1,20,"London",NULL),
(1,1,1,32,"Paris",NULL),
(1,1,1,11,"Berlin",NULL),
(1,1,2,20,"Newton",1),
(1,1,2,32,"Newton",1),
(1,1,2,32,"Isaac",0),
(1,1,2,11,"Isaac",NULL),
(1,1,2,11,"Newton",NULL),
(1,1,2,11,"Isaac Newton",NULL);

# I want a short UPDATE SET query to populate the NULL values
# after establishing rules indicated by the M field.
# For example, where S=1 and Q=1 and R=1 then we can establish that
# Where RES=London, M=1 and where RES=Paris, M=0
# I understand it needs to be an UPDATE SET statement with a subquery.
# NULLs should be retained where a rule cannot be established.
# The table is built in such a way that there will never be a conflict
# e.g. for RES=London, M will always equal 0 or NULL where S=1 and Q=1 and R=1
# The query should not reference specific values for any field
# The query needs to update all the NULLs where it can,
# and leave the NULLs as they are where it can't.
# the P field is not relevant, but is included to show there is no primary key.

# Secondly, I have the following table

CREATE TABLE QUA (S int, R int, Q int, ANS varchar(100));

# With the following contents:

INSERT INTO QUA (S,R,Q,ANS) VALUES
(1,1,1,"London"),
(1,1,2,"Isaac Newton");

# This establishes rules for populating the RES table with 1s in the M column.
# Where RES.M is NULL, a second UPDATE SET query should update the RES table.
# It should leave other NULLs as they are.

# Please provide two query lines that can be run successfully on http://sqlfiddle.com

# Many thanks.
0
Comment
Question by:Mondorica
1 Comment
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 38747815
It sounds like you need a conditional UPDATE.
For example, the following updates M based on the value of RES.

UPDATE RES
SET M = CASE RES WHEN 'London' THEN 1 WHEN 'Paris' THEN 0 ELSE M END
WHERE S=1 AND Q=1 AND R=1
;

Open in new window


I am not sure I understand the caveat to not specify values in the query. The way it reads is as if the values come from the QUA table. If that is the case, then you will need an UPDATE with a JOIN.

UPDATE RES r
JOIN QUA q ON r.S=q.S AND r.Q=q.Q AND r.R=q.R
SET r.M = CASE r.RES WHEN q.RES THEN ... ELSE r.M END
;

Open in new window


I do not see a column in QUA that has the 0 or 1, but at least you should have the general idea. The M in the ELSE ensures that the current value, including NULL, stays if no options are met. If the value reverts to NULL, then use no ELSE condition (e.g., CASE x WHEN y THEN z END).

I hope that helps!

Best regards and happy coding,

Kevin
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now