1

我这里有问题;当我进行查询时:

SELECT product.*,product_description.*,COUNT(product_image.*) 
FROM " . DB_PREFIX . "product  
INNER JOIN product_description ON product.product_id = product_description.product_id 
INNER JOIN product_image ON product.product_id = product_image.product_id 
ORDER BY product.date_modified DESC

我想为表 product_image 选择 3 个表 n 1 个计数,但出现此错误:

注意:错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行错误号:1064 SELECT product. ,产品描述。,COUNT(product_image. ) FROM product INNER JOIN product_description ON product.product_id = product_description.product_id INNER JOIN product_image ON product.product_id = product_image.product_id ORDER BY product.date_modified DESC

4

3 回答 3

1

你不应该使用COUNT这样的功能。COUNT(TableName.*)在加入表格时。你需要使用它的任何一个这样的列COUNT(TableName.ColumnName)

例如COUNT(product_image.ID)

看到这个 SQLFiddle

于 2012-12-05T10:01:39.793 回答
0

使用派生查询进行计数

SELECT 
    product.*,
    product_description.*,
    pi.Count as T_Count
FROM " . DB_PREFIX . "product  
INNER JOIN product_description ON product.product_id = product_description.product_id 
INNER JOIN (SELECT product_id , count(*) as Count FROM product_image) as pi ON product.product_id = pi.product_id 
ORDER BY product.date_modified DESC
于 2012-12-05T10:02:54.027 回答
0

尝试这个::

SELECT 
product.*,
product_description.*,
COUNT(1) 
FROM " . DB_PREFIX . "product  
INNER JOIN product_description ON product.product_id = product_description.product_id 
INNER JOIN product_image ON product.product_id = product_image.product_id 
ORDER BY product.date_modified DESC
于 2012-12-05T10:08:24.233 回答