Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL UPDATE SET query

Posted on 2013-01-05
1
Medium Priority
?
627 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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