简体   繁体   English

MariaDB SELECT 和 Update 的正确 SQL 语法

[英]Correct SQL Syntax for MariaDB SELECT and Update

I am trying to figure out the correct syntax for a SQL query that is currently in MS SQL Server syntax and needs to be re-written in syntax that will work with MariaDB.我正在尝试找出当前使用 MS SQL Server 语法的 SQL 查询的正确语法,并且需要使用适用于 MariaDB 的语法重新编写。

I need to to run a SELECT statement, and then run an update based on the results.我需要运行 SELECT 语句,然后根据结果运行更新。

This is what I've tried for the MariaDB re-write:这是我为 MariaDB 重写所做的尝试:

SELECT
  status
FROM
  addynamics.active_status
WHERE
  "table" = "dynamics_time"

IF (status = 3)
    UPDATE
      active_status
    SET
      status = 4
    WHERE
      "table" = 'dynamics_time' 
 END IF;

But this is giving me a syntax error.但这给了我一个语法错误。 What should this look like?这应该是什么样子?

As i understand your requirements correct, your statement should looks like:据我了解您的要求是正确的,您的陈述应如下所示:

update addynamics.active_status
set  status = 4
WHERE `table` = "dynamics_time" and status=3

You're trying to do two separate things here when you could do them as one.当你可以将它们作为一件事来做时,你试图在这里做两件不同的事情。 You don't need to SELECT anything to be able to do an UPDATE unless you want to do it before just to make sure you have the right data.您不需要 SELECT 任何内容来执行 UPDATE,除非您想在之前执行此操作以确保您拥有正确的数据。

Like Jens @Jens said, doing this would give you the desired results:就像 Jens @Jens 说的,这样做会给你想要的结果:

UPDATE addynamics.active_status
SET STATUS = 4
WHERE `table` = 'dynamics_time' and status = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM