[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Mysql complex update syntax

Posted on 2010-09-21
3
Medium Priority
?
527 Views
Last Modified: 2012-05-10
I have a table with pairs of records, each can be tied together via an ID number.  Each pair has a time value where 1 record might be the same or later in time than its pair.  I want to update the check_time field where the newer time = 'Y' and the older time can stay the default 'N'.  If the times are the same, I want to use a different field that happens to be boolean (0,1) to determine the check_time field, something like if boolean_field = 0 set check_time = 'Y'

I'll try to create sample table data in code section.
record | shared_id | boolean_field | time | check_time
1      |  25       |  0            | 3:01 | Y
2      |  25       |  1            | 3:00 | N

Open in new window

0
Comment
Question by:mcgilljd
[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
3 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 33730342
Because of your complex tiebreak - time, then boolean_field, a 3 level subquery is required.
Please see below.
update qm
left join
(
select q3.record
from (
	select q2.shared_id, MT.maxtime, Min(q2.boolean_field) minbool
	from (
		select shared_id, max(time) maxtime
		from qm q1
		group by shared_id) MT
	inner join qm q2 on q2.shared_id=MT.shared_id and q2.time=MT.maxtime
	group by q2.shared_id, MT.maxtime) mb
inner join qm q3 on mb.shared_id=q3.shared_id and mb.maxtime=q3.time and mb.minbool=q3.boolean_field
) qx on qx.record=qm.record
set check_time = case when qx.record is null then 'N' else 'Y' end

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33738955
Here is another approach that might help:

create table qm(record int,shared_id int,boolean_field tinyint(1),`time` time, check_time char(1));

insert into qm
values(1, 25, 0, '03:01', 'N'),
(2, 25, 1, '03:00', 'N');

Might be same was what cyberkiwi is doing above, I haven't checked...
update qm q1
join (
   select shared_id, max(`time`) as max_time
   from qm 
   group by shared_id
) q3 on q1.`time` = q3.max_time
   and q3.shared_id = q1.shared_id
join qm q2 on q2.shared_id = q1.shared_id
   and q2.record <> q1.record
set q1.check_time = 
   case 
      when (q1.`time` > q2.`time`) 
        or (q1.`time` = q2.`time` and q1.boolean_field = 0) 
        then 'Y' 
      else 'N' 
   end
;

Open in new window

0
 

Author Closing Comment

by:mcgilljd
ID: 33744854
Wow!
That was awesome!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
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…
Suggested Courses

649 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