我知道我不能在MySQL的一个视图的子句中做一个子查询。 (错误1349)
这个简单的陈述是否有任何解决方法。
CREATE VIEW view as
SELECT * FROM
(
SELECT Credits.ID,Credits,Bonus
FROM Credits,Bonus
WHERE Credits.ID = Bonus.ID
ORDER BY Credits.Date DESC, Bonus.Date DESC
) as tmp
GROUP BY ID
这是我想要的
ID Credits Bonus
1 1300 2
2 23 40
3 3045 134
我通过相关的sql查询解决了这个问题。
答案 0 :(得分:1)
是。首先,正确编写查询。您的查询取决于实际在子查询中工作的order by
,尽管MySQL不保证这一点。
我推测您需要credits
的记录以及最新的bonus
记录。如果没有样本数据,所需结果和数据布局,则很难解释查询。
相反,您应该使用not exists
:
CREATE VIEW view as
select c.ID, c.Credits, b.Bonus
from credits c join
bonus b
on c.id = b.id
where not exists (select 1
from bonus b2
where b2.id = b.id and b2.date > b.date
);
where
(以及select
子句中允许使用子查询。