mysql 在列中存储子查询并执行where条件
mysql storing subquery in column and performing where condition
我正在 mysql 中执行子查询,类似于
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
我应该如何在 where 子句中获取数据。可能吗
一种方法是:
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
如您所见,我已经为您的查询创建了派生 table t
,现在您的查询结果被视为 Table(temp table) 并且列为 col1、col2 和 col3,使用此结果集我们可以在 where 子句中访问 col3。
注意 - 假设 select col3 from table2
returns 根据 OP 的评论
单个值
使用cross join
:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;
我正在 mysql 中执行子查询,类似于
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
我应该如何在 where 子句中获取数据。可能吗
一种方法是:
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
如您所见,我已经为您的查询创建了派生 table t
,现在您的查询结果被视为 Table(temp table) 并且列为 col1、col2 和 col3,使用此结果集我们可以在 where 子句中访问 col3。
注意 - 假设 select col3 from table2
returns 根据 OP 的评论
使用cross join
:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;