doujianzhi3358 2017-06-30 14:14
浏览 82
已采纳

使用SQL,如果值不相等,我如何更新一列的字段值以匹配不同列的字段值?

Using the example table below, as of now I have: UPDATE wc_bidsys_picklist SET vipplays = 'YES' WHERE playoftheday = 'YES';
Is this much correct? If so how can I also add: "within a specific range that is set by the column (game_date) field values?" Range between '2016-06-29 00:00:00' through '2017-06-29 00:00:00'. Is this even possible?

My specific question in detail: (partially answered above)
Using my example below for a table called 'wc_bidsys_picklist': Using SQL is there a way to update the value ('NO' to 'YES') of each field in a specific column (vipplays) only if it does NOT equal the field of a different specific column (playoftheday) so the fields in both of these columns end up having matching values ('YES') while within a specific range set by a 3rd column (game_date)?

In more simple terms I need to say "In column (game_date) from '2016-06-29 00:00:00' through '2017-06-29 00:00:00' if the field value in the column (playoftheday) = 'YES' and the field value in the column (vipplays) = 'NO' then UPDATE the field value in the column (vipplays) to 'YES'. How, if possible, can this be written in SQL?

UPDATE: After a few updates to the fiddle this one seems to work.
My fiddle: http://sqlfiddle.com/#!9/69cf4/1/0

CREATE TABLE wc_bidsys_picklist
    (`id` int, `game_date` datetime, `playoftheday` varchar(5), `vipplays` varchar(5))
;

INSERT INTO wc_bidsys_picklist
    (`id`, `game_date`, `playoftheday`, `vipplays`)
VALUES
    (1, '2016-07-01 00:00:00', 'YES', 'NO'),
    (2, '2016-07-03 00:00:00', 'YES', 'YES'),
    (3, '2016-07-04 00:00:00', 'YES', 'NO'),
    (4, '2016-07-04 00:00:00', 'YES', 'NO'),
    (5, '2016-07-06 00:00:00', 'YES', 'NO'),
    (6, '2016-07-07 00:00:00', 'YES', 'YES'),
    (7, '2017-06-08 00:00:00', 'YES', 'NO'),
    (8, '2017-06-29 00:00:00', 'YES', 'NO')
;
UPDATE 
  wc_bidsys_picklist
SET 
  vipplays = 'YES' 
WHERE 
  playoftheday = 'YES'
AND
  (game_date BETWEEN '2016-07-01 00:00:00' AND '2017-06-29 00:00:00');


On a side note I would like to reiterate that I am relatively new to this community and programming. I don't understand why all the immediate down votes. It seems like members are more interested in correcting posts and bullying new users rather than helping you out. And apparently I'm not the only one: https://hackernoon.com/the-decline-of-stack-overflow-7cb69faa575d It's an interesting read.

  • 写回答

1条回答 默认 最新

  • doupo1865 2017-06-30 14:20
    关注

    I think this this will work:

    UPDATE wc_bidsys_picklist SET vipplays = 'YES' WHERE playoftheday = 'YES' AND game_date='2016-07-20 00:00:00';

    Change the date as needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作