此查询出现SQL错误
错误号:1064
您的SQL语法有误;查看与您的MySQL服务器版本相对应的手册,以在'*)附近使用正确的语法
来自`cast_gallery` q
在q.all_media_id = r.id上加入`all_media` r
在第1行的q.pa'
SELECT `cast_name`, `cast_slug`, (SELECT count(q.*)
FROM `cast_gallery` q
JOIN `all_media` r on q.all_media_id=r.id
WHERE q.parent = `casts`.`id` AND r.approved=1) as image_gallery, `active`, `view`, `checked`, `sex`, `id`
FROM (`casts`)
ORDER BY `cast_name` asc
LIMIT 10
如何解决?
答案 0 :(得分:1)
要计算总行数,我们使用COUNT(*)
,而不是Count(q.*)
。另外,casts
子句中的From
周围也不需要括号。
SELECT `cast_name`,
`cast_slug`,
(SELECT count(*)
FROM `cast_gallery` q
JOIN `all_media` r on q.all_media_id=r.id
WHERE q.parent = `casts`.`id` AND
r.approved=1) as image_gallery,
`active`,
`view`,
`checked`,
`sex`,
`id`
FROM `casts`
ORDER BY `cast_name` asc
LIMIT 10
其他信息:
COUNT(...)
用于计算非NULL 值的数量;但是COUNT(*)
将计算检索到的行总数,无论它们是否包含NULL值。
答案 1 :(得分:1)
尝试仅将count(q.*)
更改为count(*)
答案 2 :(得分:0)
计数语法不正确。如果要在子查询中查找行数,可以使用count(*)。有关更多说明,请参阅http://www-db.deis.unibo.it/courses/TW/DOCS/w3schools/sql/sql_func_count.asp.html。
SELECT `cast_name`, `cast_slug`, (SELECT count(*)
FROM `cast_gallery` q
JOIN `all_media` r on q.all_media_id=r.id
WHERE q.parent = `casts`.`id` AND r.approved=1) as image_gallery, `active`,
`view`, `checked`, `sex`, `id`
FROM (`casts`)
ORDER BY `cast_name` asc
LIMIT 10
应该工作
答案 3 :(得分:0)
您可以尝试使用q.all_media_id代替count(q。*)
SELECT `cast_name`, `cast_slug`, (SELECT count(q.all_media_id)
FROM `cast_gallery` q
JOIN `all_media` r on q.all_media_id=r.id
WHERE q.parent = `casts`.`id` AND r.approved=1) as image_gallery, `active`, `view`, `checked`, `sex`, `id`
FROM (`casts`)
ORDER BY `cast_name` asc
LIMIT 10