【问题标题】:How to updated a column value, with the help of selected row rows in MariaDB?如何在 MariaDB 中的选定行的帮助下更新列值?
【发布时间】:2015-02-06 23:32:10
【问题描述】:

我是 MariaDB 的新手。

我有一张桌子。每行都有自己的有效性。

CREATE TABLE `tariff_table` (
  `transportation_id` int(11) NOT NULL AUTO_INCREMENT,
  `transporter` varchar(300) NOT NULL,
  `valid_upto` date NOT NULL,
  `expired_status` int(11) NOT NULL DEFAULT '0',
  `status` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`transportation_id`)
  )

表格结果:

tariff_id || transporter || valid_upto || expired_status || status   
------------------------------------------------------------------
1         || Raj         || 2014-12-08 ||             0  ||     0
2         || Ram         || 2015-01-10 ||             0  ||     0
3         || Mani        || 2014-03-06 ||             0  ||     0
4         || Mano        || 2015-04-15 ||             0  ||     0

我的要求是,如何使用选择查询更新过期状态。

select tariff_id from tariff_table where valid_upto <= DATE(now());

选择查询返回 2 行。

tariff_id
---------
1
3

id的帮助下,我需要更新如下。

我需要以下结果。

tariff_id || transporter || valid_upto || expired_status || status   
------------------------------------------------------------------
1         || Raj         || 2014-12-08 ||             1  ||     0
3         || Mani        || 2014-03-06 ||             1  ||     0   

帮帮我。

【问题讨论】:

    标签: mysql sql-update mariadb


    【解决方案1】:

    您可以使用UPDATE 语句,如下所示:

    UPDATE `tariff_table`
    SET `expired_status` = 1
    WHERE valid_upto <= DATE(now());
    

    【讨论】:

      【解决方案2】:

      使用IN 关键字。

      UPDATE tariff_table SET expired_status = 1 WHERE tariff_id 
      IN(select tariff_id from tariff_table where valid_upto <= DATE(now()))
      

      UPDATE tariff_table SET expired_status = 1 WHERE valid_upto <= DATE(now())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-30
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多