计算同一查询中不同表的行

时间:2013-07-24 11:33:18

标签: mysql sql count

我以为我之前做过类似的事情:

$reg = mysql_query ("(SELECT count(*) from vouchers) as total_vouchers), 
                     (SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
                     (SELECT count(*) from crm) as crm_users,
                     (SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());

但它会在第一个,附近返回mysql_error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as total_vouchers), ((SELECT count(*) from vouchers WHERE asignado = 1) a' at line 1

知道为什么吗?

1 个答案:

答案 0 :(得分:3)

尝试添加select,如下所示

$reg = mysql_query ("SELECT
                     (SELECT count(*) from vouchers) as total_vouchers, 
                     (SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
                     (SELECT count(*) from crm) as crm_users,
                     (SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());