Solved

MySQL UPDATE SET query

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now