假设以下查询:
select id, (select count(*) from items) as item_count,
items_stored - (select count(*) from items) from something
当我签出时,不可能以下列方式重用item_count:
select id, (select count(*) from items) as item_count, items_stored - item_count from something
由于MySQL将打印未定义的列错误。
但是,我的问题是 - 使用下面定义的select语句 - 会使它执行两次,从而使查询更慢吗?或者它是否被隐藏在引擎盖下(结果是相同的)。
(select count(*) from items)
双重用法示例:
select id, (select count(*) from items) as item_count,
items_stored - (select count(*) from items) from something
答案 0 :(得分:0)
您可以像这样重写查询:
select
tbl.id,
tbl.item_count,
tbl.items_stored - tbl.item_count
from
(
SELECT
something.id,
something.items_stored,
(select count(*) from items) as item_count
FROM
something
) as tbl
你计算每行中的一些