【发布时间】:2018-11-17 05:20:18
【问题描述】:
我需要在一个查询中从 mysql 5.7.22 中进行选择。
select id from t1 where type_id=(select type_id from t2 where id=1 limit 1) and id not in
(select obj_id from t2
where
type_id = (select type_id from t2 where id=1 limit 1)
and
type2_id = (select type2_id from t2 where id=1 limit
...
)
我在 where 子句中有一些重复的子查询(它只是查询的一部分,这个子查询重复了很多次)
'(select type_id from t2 where id=1 limit 1)'
我可以在一个地方弄清楚它,以减少冗长。 所以我想选择一次
select type_id, type2_id from t2 where id=1 limit 1
并使 type_id、type2_id 在所有查询上下文中可用。
我知道 mysql 8.0 有 WITH 语法,但我使用的是 5.7.22
我想在一个没有事务的查询中执行此操作。
【问题讨论】: