gpt4 book ai didi

mysql - 将多个选择与更新相结合

转载 作者:行者123 更新时间:2023-11-30 22:52:29 25 4
gpt4 key购买 nike

我有一个拖累我的应用程序的查询。必须有一种方法可以通过将更新与选择结合起来来改进这一点。任何帮助表示赞赏。这是非常慢的查询:

Select t1.id, t2.tracker from Table1 t1, Table2 t2 where t1.id=t2.id and t2.tracker is not null

上面返回了一个我可以处理的行集。对于返回的每一行,我检查跟踪器第三个表,看看是否只有 1 行存在:

select tracker from Table3 where tracker="from above query". 

如果跟踪器在表 3 中有 1 行的计数,那么我将对表 1 执行更新。

Update Table 1, set some field where id=t1.id

我该如何结合呢?列出的答案很棒,但我想问题还不够清楚。因此,我编辑了问题。

表 1 返回可能要更新的 ID 列表。表 2 返回我需要搜索表 3 的跟踪器。表 3 告诉我跟踪器是否只存在一次,因此我可以使用它返回表 1 并更新它。

最佳答案

通过使用 HAVING 子句和附加的 LEFT JOIN,您可以将前两个查询合并为一个返回 tracker 的查询Table3 中有一条记录,表达式如 HAVING COUNT(t3.tracker) = 1

SELECT 部分看起来像:

SELECT
t1.id,
t2.tracker
FROM
-- Your original comma-separated FROM clause (implicit inner join)
-- has been replaced with a more modern explicit INNER JOIN, which
-- works more clearly with the LEFT JOIN we need to do.
Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
-- left join table 3 by tracker value
LEFT JOIN Table3 t3 ON t2.tracker = t3.tracker
WHERE t2.tracker IS NOT NULL
GROUP BY
t1.id,
t2.tracker
-- limit the group to only those with 1 tracker in t3
HAVING COUNT(t3.tracker) = 1

现在,您应该能够使用JOIN 将其填充到UPDATE 查询中。 MySQL 的更新连接语法如下所示:

UPDATE 
Table1 t_up
-- join Table1 in its normal form against the query from above
-- MySQL won't allow an IN () subquery for update in most versions
-- so it has to be done as a join instead.
JOIN (
-- the subquery only needs to return t1.id
SELECT t1.id
FROM
Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
LEFT JOIN Table3 t3 ON t2.tracker = t3.tracker
-- Filter to those with non-null t2.tracker
WHERE t2.tracker IS NOT NULL
GROUP BY
-- Since only id was in SELECT, only id needs to be in GROUP BY
t1.id
HAVING COUNT(t3.tracker) = 1
) t_set ON t_up.id = t_set.id
SET t_up.some_field = 'SOME NEW VALUE'

Here is a demonstration of the concept in action...

关于mysql - 将多个选择与更新相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com