结合更新和选择查询 [英] Combine Update and Select Query

查看:53
本文介绍了结合更新和选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 MySQL 工作正常,我正在尝试找到一种方法将它们组合成一个查询.

I got two MySQL working fine and i'm trying to find a way to combine them into one single query.

首先,它选择员工的 ID.

First, it selects ID of an employee.

SELECT 'ID' FROM `employee` ORDER BY ID DESC LIMIT 1;

假设它返回 ID 100;

Let's say it returns ID 100;

然后更新ID为100的员工数据

Then update data of employees whose ID is 100

UPDATE 'LOG' SET `TIME_EXIT`='2013/02/22' WHERE `ID`='100';

我可以在一个查询中完成所有操作吗?

Can i do it all in a single query?

推荐答案

只需将它们加在一起:

UPDATE LOG SET TIME_EXIT = '2013/02/22' 
WHERE ID = (
              SELECT ID
              FROM employee
              ORDER BY ID DESC
              LIMIT 
            );

但根据目前的代码,它只会更新最后一个 employee,您需要使用其他一些标识符来选择正确的 employee 以确保您拥有正确的.

But based on that code currently it'll only ever update the last employee, you will need to select the correct employee by using some other identifier to ensure you have the correct one.

UPDATE LOG SET TIME_EXIT = '2013/02/22' 
WHERE ID = (
              SELECT ID 
              FROM employee 
              WHERE NAME = 'JOHN SMITH' 
              ORDER BY ID DESC 
              LIMIT 1
            );

这篇关于结合更新和选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆