【问题标题】:SQL Update QuerySQL 更新查询
【发布时间】:2015-05-20 00:07:26
【问题描述】:

我的数据库中有 40,000 行需要更新。但是,我使用的选择查询跨越两个表。

我的选择查询示例如下:

select t1.*, t2.data, t2.more_data, t2.more_data2 
from table1 as t1, table2 as t2
where 
DATE(t1.date) >= '2014-10-23'
and
t1.direction = 10
and
t1.date_read is NULL
and
t1.fk_client is NULL
and
t1.id=t2.id
and
t2.data = 'this is dummy text';

我一直在查看其他人对此的要求,但我似乎无法理解它。

编辑:

我想要做的是用2015-03-17 09:00:00 更新t1.date_read,其中t2.data 等于'this is dummy test'

【问题讨论】:

  • 您要更新哪些列以及使用什么值?
  • 那么你的问题是什么?
  • 没有t3 表在您的t3.more_data2 的查询中声明
  • 我可以看到您的查询存在多个问题...SELECT 中缺少 .,日期和数据字段中缺少右引号,缺少表引用 (t3),不寻常的 @ 987654330@ 对名为 date 的列进行强制转换,使用已被弃用 20 多年的 JOIN 语法...但是您的问题到底是什么?
  • 字符串以单引号开头,但结尾引号不存在

标签: mysql sql


【解决方案1】:

首先你的查询中有一些错误

  1. t1* 缺少点 -> t1.*

  2. t3.more_data2 - 查询中未声明 t3

  3. t2.data = 'this is dummy text; 错过收盘价

  4. 连接表键不明确

所以试试这个更正的查询并尝试解释你的目标:

SELECT
   t1.*,
   t2.data, 
   t2.more_data
FROM
   table1 as t1, 
INNER JOIN
   (SELECT *
    FROM   table2 
    WHERE
      data = 'this is dummy text'
   ) as t2
ON 
   t1.id=t2.id
WHERE 
   DATE(t1.date) >= '2014-10-23
   AND
   t1.direction = 10
   AND
   t1.date_read is NULL
   AND
   t1.fk_client is NULL

【讨论】:

    【解决方案2】:

    我想要做的是用2015-03-17 09:00:00 更新t1.date_read,其中t2.data 等于'this is dummy test'

    只需在更新的 WHERE 子句中引用另一个表,如下所示:

    UPDATE table1
    SET date_read = '2015-03-17 09:00:00'
    where table1.id IN (SELECT id from table1 as t1
                        INNER JOIN table2 as t2 ON t1.id=t2.id
                        WHERE t2.data = 'this is dummy test')
    

    如果您想包含您在问题中输入的其他参数,只需将它们添加到末尾...

    --[code from above]
    AND
    DATE(table1.date) >= '2014-10-23'
    AND
    table1.direction = 10
    AND
    table1.date_read is NULL
    AND
    table1.fk_client is NULL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多