使用函数FOUND_ROWS()的问题在连接时给出了错误的结果

时间:2016-04-13 06:27:37

标签: mysql join sql-calc-found-rows

这是我的疑问。

total:344

已接受的结果为total:1,我正在获得select SQL_CALC_FOUND_ROWS id,(FOUND_ROWS() ) as total from unique_url_master1 limit 10;

我执行了这个查询,我得到了正确的结果。

total:1313

SELECT count(*) as total_count , (SELECT GROUP_CONCAT(sp.specialization) FROM DSpecialization_Master dsp LEFT JOIN Specialization_Master sp on sp.id = dsp.specialization WHERE dsp.profileid = pm.id and (dsp.specialization = (select id from Specialization_master where specialization='Dentist' ))) as drspec, pm.id as profileid, pm.loginid as loginid, dam.clinicname, dam.area, dam.address, dam.pincode, dam.id as addressid, dam.feecharge as feecharge, pm.fname, pm.lname, pm.email, pm.mobile, pm.phone, pm.gender, pm.dob, pm.totexp, pm.imagepath, pm.languages, pm.statement, pm.createdby, um.profile_url, um.clinic_url, pm.hsbit, (SELECT GROUP_CONCAT(education) FROM DEducation_Master WHERE profileid = pm.id ) as dredu FROM Profile_Master pm LEFT JOIN DAddress_Master dam on dam.profileid = pm.id LEFT JOIN Unique_Url_Master um on um.clinicid =dam.id WHERE dam.city='Surat' AND pm.id IN (SELECT profileid FROM DSpecialization_Master WHERE specialization = (select id from Specialization_master where specialization='Dentist')) ORDER BY pm.id LIMIT 0, 10;

我在做什么?仅供参考我不能破解查询,即先执行

  

选择SQL_CALC_FOUND_ROWS

并执行

  

选择FOUND_ROWS()

非常需要帮助......

修改

我更改了查询并使用了count(*)函数。现在我正确总计:344 ,但它只返回一行。现在有人可以指导我吗?

{{1}}

我已经使用COUNT(*)OVER()在MSSQL中进行查询,因为需求更改我必须将MSSQL查询转换为MYSQl。结果我接受这个this

1 个答案:

答案 0 :(得分:1)

您正在使用不带n COUNT的聚合函数GROUP BY,这将始终导致一行。 Definition of GROUP BY

最好只使用2个单独的查询来获得你想要的东西,一个你选择所有其他变量,一个只选择计数

所有行+变量

SELECT  (SELECT GROUP_CONCAT(sp.specialization) FROM DSpecialization_Master dsp LEFT JOIN Specialization_Master sp on sp.id = dsp.specialization WHERE dsp.profileid = pm.id and (dsp.specialization = (select id from Specialization_master where specialization='Dentist' ))) as drspec, 
    pm.id as profileid, 
    pm.loginid as loginid, 
    dam.clinicname, 
    dam.area, 
    dam.address, 
    dam.pincode, 
    dam.id as addressid, 
    dam.feecharge as feecharge, 
    pm.fname, 
    pm.lname, 
    pm.email, 
    pm.mobile, 
    pm.phone, 
    pm.gender, 
    pm.dob, 
    pm.totexp, 
    pm.imagepath, 
    pm.languages, 
    pm.statement, 
    pm.createdby, 
    um.profile_url, 
    um.clinic_url, 
    pm.hsbit,  
    (SELECT GROUP_CONCAT(education) FROM DEducation_Master WHERE profileid = pm.id ) as dredu 
FROM Profile_Master pm 
LEFT JOIN DAddress_Master dam on dam.profileid = pm.id 
LEFT JOIN Unique_Url_Master um on um.clinicid =dam.id 
WHERE dam.city='Surat' 
AND pm.id IN (SELECT profileid FROM DSpecialization_Master WHERE specialization = (select id from Specialization_master where specialization='Dentist')) 
ORDER BY pm.id 
LIMIT 0, 10;

计数:

SELECT  count(*) as total_count,
FROM Profile_Master pm 
LEFT JOIN DAddress_Master dam on dam.profileid = pm.id 
LEFT JOIN Unique_Url_Master um on um.clinicid =dam.id 
WHERE dam.city='Surat' 
AND pm.id IN (SELECT profileid FROM DSpecialization_Master WHERE specialization = (select id from Specialization_master where specialization='Dentist')) 
ORDER BY pm.id 
LIMIT 0, 10;

如果您真的希望它在一个查询中与您的图像一样,则必须执行以下操作:

SELECT T1.overall_count, T2.*
FROM (SELECT COUNT(*) AS overall_count
    FROM Profile_Master pm 
    LEFT JOIN DAddress_Master dam on dam.profileid = pm.id 
    LEFT JOIN Unique_Url_Master um on um.clinicid =dam.id 
    WHERE dam.city='Surat' 
    AND pm.id IN (SELECT profileid FROM DSpecialization_Master WHERE specialization = (select id from Specialization_master where specialization='Dentist')) 
    ORDER BY pm.id 
    LIMIT 0, 10) AS T1
JOIN ((SELECT GROUP_CONCAT(sp.specialization) FROM DSpecialization_Master dsp LEFT JOIN Specialization_Master sp on sp.id = dsp.specialization WHERE dsp.profileid = pm.id and (dsp.specialization = (select id from Specialization_master where specialization='Dentist' ))) as drspec, 
    pm.id as profileid, 
    pm.loginid as loginid, 
    dam.clinicname, 
    dam.area, 
    dam.address, 
    dam.pincode, 
    dam.id as addressid, 
    dam.feecharge as feecharge, 
    pm.fname, 
    pm.lname, 
    pm.email, 
    pm.mobile, 
    pm.phone, 
    pm.gender, 
    pm.dob, 
    pm.totexp, 
    pm.imagepath, 
    pm.languages, 
    pm.statement, 
    pm.createdby, 
    um.profile_url, 
    um.clinic_url, 
    pm.hsbit,  
    (SELECT GROUP_CONCAT(education) FROM DEducation_Master WHERE profileid = pm.id ) as dredu 
FROM Profile_Master pm 
LEFT JOIN DAddress_Master dam on dam.profileid = pm.id 
LEFT JOIN Unique_Url_Master um on um.clinicid =dam.id 
WHERE dam.city='Surat' 
AND pm.id IN (SELECT profileid FROM DSpecialization_Master WHERE specialization = (select id from Specialization_master where specialization='Dentist')) 
ORDER BY pm.id 
LIMIT 0, 10) AS T2