mysql重复更新不工作?

u0njafvf  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(279)

movie_title 是唯一的列。我想更新重复的列。查询中没有错误。

foreach($array['data'] as $row) {

        $movie_title = $row[0];
        $time_published = date("Y-m-d H:i:s",strtotime($row[2]));
        $language = $row[3];
        $views = $row[4];
        $active = $row[6];
        $type = $row[7];
        $checked = $row[8];
        $category = $row[9];
        $rating = $row[10];
        $subtitle = $row[12];
        $visitor_views = $row[18];

        if($visitor_views!=0) {
            $view_array[] = $visitor_views;
        }

        $movie_title = explode("-",$movie_title);

        $movie_title = trim($movie_title[0]);

          $wpdb->query($wpdb->prepare("INSERT INTO movies (movie_title,time_published,language,views,visitor_views,active,type,checked,category,rating,subtitle) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE active=active,category=category,views=views,visitor_views=visitor_views,checked=checked,subtitle=subtitle,rating=rating,type=type,language=language",
            $movie_title,$time_published,$language,$views,$visitor_views,$active,$type,$checked,$category,$rating,$subtitle));

    }

这就是查询的外观

INSERT INTO movies (movie_title,time_published,language,views,visitor_views,active,
    type,checked,category,rating,subtitle)
VALUES
    ('Bodyguard','2016-12-17 06:54:26','1','2091','15',
    '1','0','0','Feature Film ','3.66','Bodyguard.vtt')
ON DUPLICATE KEY UPDATE
    active=active,category=category,views=views,visitor_views=visitor_views,
    checked=checked,subtitle=subtitle,rating=rating,type=type,language=language

为什么没有更新任何列?

szqfcxe2

szqfcxe21#

不确定这是否解决了问题,但也许应该是这样:

ON DUPLICATE KEY UPDATE active = VALUES(active), category = VALUES(category)..
bvhaajcl

bvhaajcl2#

你需要使用 col_name = VALUES(col_name) ,否则您只是说“设置x=x”。

... 
ON DUPLICATE KEY UPDATE
active=VALUES(active),category=VALUES(category),views=VALUES(views),...

相关问题