如何获取 VIEW 中的单个字段以在 WHERE 中使用
How to get a single field in a VIEW to use in WHERE
亲爱的堆栈溢出朋友,您好,
我想使用位于其他视图中的单个字段作为 WHERE 子句(在其他视图中)的参数。但是,我找不到完成此操作的正确方法。
我已经使用了 FIRST() 函数,但这给出了语法错误。
这可能与它选择了整个第一行有关,但我只想获得这一行中的一个字段。 (实际上整个视图只有一个字段)
/*this first view contains the value to use for the second view, called average */
CREATE OR REPLACE VIEW getAverage AS
SELECT AVG(todoitem.completiondate-todoitem.creationdate) as average from todolist
inner join todoitem on todoitem.id=todolist.id;
/*this view has a WHERE clause that should use a value in the getAverage VIEW */
CREATE OR REPLACE VIEW aboveAverage AS
SELECT * FROM todolist
inner join todoitem on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > FIRST(getAverage.average));
如果有人能帮助我,我将不胜感激。
这是你想要的吗?
CREATE OR REPLACE VIEW aboveAverage AS
SELECT *
FROM todolist inner join
todoitem
on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > (SELECT average FROM getAverage);
MySQL 确实允许 where
和 select
子句中的子查询。您不需要第二个视图。
亲爱的堆栈溢出朋友,您好,
我想使用位于其他视图中的单个字段作为 WHERE 子句(在其他视图中)的参数。但是,我找不到完成此操作的正确方法。
我已经使用了 FIRST() 函数,但这给出了语法错误。
这可能与它选择了整个第一行有关,但我只想获得这一行中的一个字段。 (实际上整个视图只有一个字段)
/*this first view contains the value to use for the second view, called average */
CREATE OR REPLACE VIEW getAverage AS
SELECT AVG(todoitem.completiondate-todoitem.creationdate) as average from todolist
inner join todoitem on todoitem.id=todolist.id;
/*this view has a WHERE clause that should use a value in the getAverage VIEW */
CREATE OR REPLACE VIEW aboveAverage AS
SELECT * FROM todolist
inner join todoitem on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > FIRST(getAverage.average));
如果有人能帮助我,我将不胜感激。
这是你想要的吗?
CREATE OR REPLACE VIEW aboveAverage AS
SELECT *
FROM todolist inner join
todoitem
on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > (SELECT average FROM getAverage);
MySQL 确实允许 where
和 select
子句中的子查询。您不需要第二个视图。