2016-04-13 16 views
0

Bu benim sorgu listem. Kabul sonuç total:344 olduğunuİşlevler kullanılarak sorunlar FOUND_ROWS() yanlış bir sonuç veren katılım

SELECT SQL_CALC_FOUND_ROWS (FOUND_ROWS()) as total, 
(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 

i total:1 alıyorum instated.

Bu sorguyu çalıştırıyorum ve doğru sonucu alıyorum.

select SQL_CALC_FOUND_ROWS id,(FOUND_ROWS()) as total from unique_url_master1 limit 10; 

total:1313

Neyi yanlış yapıyorum edilir? Bilginize ben ..

sorgu yani ilk

seçme SQL_CALC_FOUND_ROWS

yürütmek ve çok kötü yardıma ihtiyaç

seçme FOUND_ROWS()

yürütmek bozamam

E DIT

Sorgumu değiştirdim ve count (*) işlevini kullandım. Şimdi doğru doğru alıyorsanız: 344 ama sadece bir satır döndürün. Şimdi birisi bana rehberlik edebilir mi?

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; 

Zaten COUNT kullanımı ile MSSQL sorgu çalışma var (*) AŞIRI(), gereksinim değişikliği MySQL içine MSSQL sorgu dönüştürmek zorunda çünkü. Sonuç Ben bu her zaman bir sıra meydana getirmeleri n GROUP BY olmayan bir bütünleştirici, işlevini COUNT kullandığınız bu this

cevap

1

gibi bu kabul ediyorum. Definition of GROUP BY.

Tıpkı diğer tüm değişkenler seçip birinde yalnızca tüm satırlar + değişkenler sayımı

seçin istediğini elde etmek tane 2 ayrı sorguları kullanmak çok daha iyi olurdu

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; 

Sayısı:

0123:

gerçekten bir sorguda resminizin gibi isterseniz
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; 

, aşağıdakileri yapmak zorunda

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 
+0

bunu ayrıntılı olarak açıklayabilir misiniz? – Archish

+0

COUNT bir toplama işlevidir, sahip olduğunuz tüm satırları tek satırda toplar. elde etmeye çalıştığınız sonuç nedir? Hem tüm sonuçları hem de sorguyu istiyorsanız, 2 sorgu yapmalısınız (veya 1 sorgu yapın ama aynı şeyi elde etmek için 2 alt sorgu yapın). Sorgunuzu güzel girintiye sahip olacak şekilde değiştirirseniz, çok daha fazla okunabilir hale gelir ve ayrıca bir sql kemanı insanların neyin yanlış olduğunu anlamasına yardımcı olur. – Jester

+0

Soruma tekrar baktım lütfen göz atın – Archish

İlgili konular