我创建了一个试图从中查询的视图表

drop view if exists salesdata;

create view SalesData as
select orders.order_id, items.item_id, (order_details.order_qty *   items.unit_price) as ItemTotal, items.title, artists.artist_id
from orders
join order_details
on orders.order_id = order_details.order_id
join items
on order_details.item_id = items.item_id
join artists
on items.artist_id = artists.artist_id
order by artists.artist_id
;

select * from salesdata;

select artists.artist_id, sum(order_details.order_qty * items.unit_price) as TotalSales
from SalesData;


但是,每当我尝试完成查询时

select artists.artist_id, sum(order_details.order_qty * items.unit_price) as TotalSales
from SalesData;


生病以下错误

Error Code: 1054. Unknown column 'artists.artist_id' in 'field list'

最佳答案

您必须选择视图中描述的字段,请尝试:

select artists.artist_id, sum(ItemTotal) as TotalSales from SalesData;

关于mysql - 如何从 View 表查询/选择?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37872524/

10-11 05:22