2015-09-18 26 views
0

我得到以下錯誤而創建視圖出錯創建視圖(View的SELECT包含在FROM子句中的子查詢)請建議對這段代碼

備用選項或方法

視圖的SELECT語句包含在子查詢FROM子句

請建議爲這一段代碼

Create or Replace view mydb.cnCustomerv 
as 
Select * from mydb.customermaster 
left join 
(
    Select customertransaction.transactioninvoiceno , customertransaction.transactionvalue , customertransaction.cardnumber from mydb.customertransaction order by customertransaction.transactionid desc limit 1 
) as a on 
mydb.customermaster.cardnumber = a.cardnumber 
left join 
(
    Select customerredemption.transactionuniquecode , customerredemption.redemptionvalue , customerredemption.cardnumber from mydb.customerredemption order by customerredemption.redemptionid desc limit 1 
) as B on 
mydb.customermaster.cardnumber = B.cardnumber; 
+0

http://stackoverflow.com/questions/206062/mysql-view-with-subquery-in-the-from-clause-limitation - 這可能會幫助你,還要檢查第二通過「Json on Linux Apache My」用戶描述他的問題並尋找類似的問題。 –

+0

@Nilesh在創建子查詢視圖不工作,我已經嘗試過。 –

+0

是在子查詢上創建視圖將不起作用,但您可以通過其他方式實現它,請查看下面的答案。 –

回答

0

替代選項或方法根據手冊,VIEW不能包含子查詢。如果您想在查詢中創建VIEW,則需要爲您的子查詢創建單獨的視圖。

首先查看

Create or Replace VIEW mydb.CTView 
AS 
Select ct.transactioninvoiceno, ct.transactionvalue, ct.cardnumber 
from mydb.customertransaction ct order by ct.transactionid desc limit 1 

第二個視圖

Create or Replace VIEW mydb.CRView 
AS 
Select cr.transactionuniquecode, cr.redemptionvalue, cr.cardnumber 
from mydb.customerredemption cr order by cr.redemptionid desc limit 1 

主視圖,

Create or Replace view mydb.cnCustomerv 
as 
Select * from mydb.customermaster 
left join mydb.CTView as a on mydb.customermaster.cardnumber = a.cardnumber 
left join mydb.CRView as B on mydb.customermaster.cardnumber = B.cardnumber; 

A view definition is subject to the following restrictions:從MySQL手冊

  • SELECT語句不能在FROM子句中包含子查詢。
  • ....
+0

在執行mydb.cnCustomerv視圖時,不會提取事務&兌換表值。它只顯示客戶的主要細節 –

+0

用您需要的列替換您的'Select * from mydb.customermaster'。例如'Select customermaster。*,CTView.transactioninvoiceno,CTView.transactionvalue,CRView.transactionuniquecode,CRView.redemptionvalue from ....' –

+0

沒有它的不工作未知列CTView.transactioninvoiceno在字段列表 –

相關問題