计数查询中的Mysql错误,它计算union子查询

时间:2011-12-20 19:34:45

标签: php mysql

你可以检查一下这个mysql查询,也许可以帮助解决它有什么问题吗?

当我通过mysql作为主机运行时,我收到此错误:

#1248 - 每个派生表必须有自己的别名

以下是代码:

mysql_query("
SELECT COUNT(*) 
FROM 
(

(SELECT 1 as sort_col,id,pic0 FROM `cronjob_reloaded` WHERE id IS NOT NULL AND id LIKE '%car%') 

UNION 

(SELECT 2 as sort_col,id,pic0 FROM `cronjob_reloaded` WHERE id IS NOT NULL AND category IN ('bmw')) 

ORDER BY sort_col

)
")

泰!

PS。我前段时间发布了一个不明确的问题,管理员可以删除那个吗?很抱歉给您带来不便。问题是here

1 个答案:

答案 0 :(得分:2)

如错误所示,派生表必须是别名。

SELECT COUNT(*) 
FROM 
(

(SELECT 1 as sort_col,id,pic0 FROM `cronjob_reloaded` WHERE id IS NOT NULL AND id LIKE '%car%') 

UNION 

(SELECT 2 as sort_col,id,pic0 FROM `cronjob_reloaded` WHERE id IS NOT NULL AND category IN ('bmw')) 

ORDER BY sort_col

) q /* I added the alias "q" */