【问题标题】:SQL Join Query Assistance on mySQLmySQL 上的 SQL 连接查询辅助
【发布时间】:2015-02-16 15:34:27
【问题描述】:

这是我的基本查询

 SELECT * from ACTIONS where STARTDATE < '2015-05-01'   and EXPORTDATE is null

我需要将它与表 Contracts 连接起来,并且只包括两个表和 ENDDATE(在 Contracts 中)中 SERIAL 相同的记录

查看数据后,我需要对 ACTIONS 执行更新查询,将 STARTDATE 设置为相同记录的新日期。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    select 语句的连接:

    SELECT * from ACTIONS 
    JOIN Contracts ON Actions.Serial = Contracts.Serial
    WHERE Actions.STARTDATE < '2015-05-01' 
      AND Actions.EXPORTDATE is null
      AND Contracts.EndDate < '2015-03-01'
    

    更新(使用带有连接语法的 MySQL 更新):

    UPDATE Actions
    JOIN Contracts ON Actions.Serial = Contracts.Serial
    SET Actions.StartDate = 'NewDate'
    WHERE Actions.STARTDATE < '2015-05-01' 
      AND Actions.EXPORTDATE is null
      AND Contracts.EndDate < '2015-03-01'
    

    如果您想缩短查询,您可以为表使用别名,例如 Contracts AS C,然后使用这样的别名引用表:C.EndDate &lt; '2015-03-01'

    【讨论】:

    • 谢谢你的选择正在工作,但更新给了我一个 1064 错误,说“WHERE ..... 为空且合同”的语法不好。
    • @mloraditch 哦,对不起,我认为where 子句应该在set 语句之后。
    • Doh,我也应该抓住它的!谢谢!
    【解决方案2】:

    或者使用另一种表示法。

    SELECT * from ACTIONS act
    INNER JOIN CONTRACTS con on act.serial = con.serial 
    where con STARTDATE < '2015-05-01'   
    and act.EXPORTDATE is null
    

    【讨论】:

    • 感谢您提供更多信息!
    猜你喜欢
    • 2023-03-24
    • 2011-03-23
    • 2011-09-06
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多