基本上我有两张桌子;
Products ProductSizePrice
-------- ----------------
ID (PK) ID (PK)
Name ProductID (FK)
Size
Price
有些产品只有一个价格/尺寸,有些会有不止一个。使用PHP(5.4)和SQLite3生成产品列表和产品大小,非常简单。
$the_sql = "SELECT P.ID, P.Name, COUNT(1) AS Sizes
FROM Product AS P INNER JOIN ProductSizePrice AS PC
ON (P.ID = PC.ProductID)
GROUP BY P.ID, P.Name"
$db = new SQLite3('DatabasePathName');
$rec = $db->query($the_sql);
while ($row = $rec->fetchArray()) {
...
}
这很好用(很抱歉显而易见)。
在此SQLite3数据库中创建视图;
CREATE VIEW [ProductSizeCount] AS
SELECT [P].[ID], [P].[Name], COUNT([PC].[ID]) AS [Sizes]
FROM [Product] AS [P] INNER JOIN [ProductSizePrice] AS [PC]
ON ([P].[ID] = [PC].[ProductID])
GROUP BY [P].[ID], [P].[Name];
这样,我现在有一个View描述上面的sql语句,然后我将sql更改为;
$this_sql = "SELECT P.ID, P.Name, P.Sizes FROM ProductSizeCount AS P";
执行命令时会产生异常,
警告: SQLite3 :: query():无法准备语句:1,没有这样的表: .. \ index.php中的ProductSizeCount就行......
致命错误:在线路上 .. \ index.php中的非对象上调用成员函数fetchArray()
我的问题是,我做错了的一部分是,SQLite3 Views无法做到这一点?
谢谢和问候。