Solved

MySQL UPDATE SET query

Posted on 2013-01-05
1
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Email question 19 91
exchange 2010 Dag failed 3 66
sql views 3 55
SQL Procedure 7 49
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

734 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