Solved

MySQL UPDATE SET query

Posted on 2013-01-05
1
618 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

632 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