2012-10-08 39 views
0

我有一個查詢返回需要在列中反映其上傳文件(cloud_uploads)的25名醫生的列表。問題是,我不想限制列出的文件數量,但限制似乎也適用於這些文件。我看到的其他類似的問題也有相反的問題 - 他們想限制文件而不限制返回的總行數。我有超過25個上傳文件的醫生,但該列表僅顯示前25個文件,而不是全部文件。如何在MySQL中通過查詢來正確執行此有限組?

因爲這是一個以編程方式生成的查詢,所以我無法將其分解爲多個查詢。它需要是一個。所以,如果答案是無法完成,我可以接受。

下面是當前查詢:

SELECT SQL_CALC_FOUND_ROWS records.instance_id, 
     instances.patient_id, 
     TRIM(form_1_data.field_2) AS field_0, 
     TRIM(form_1_data.field_1) AS field_1, 
     TRIM(form_1_data.field_87) AS field_2, 
     GROUP_CONCAT(DISTINCT CONCAT (
         extensible_8_data.field_953, 
         '|||', 
         cloud_uploads.NAME 
         ) SEPARATOR '<br />') AS field_3 
FROM form_6_data AS records 
LEFT JOIN instances 
     ON instances.id = records.instance_id 
LEFT JOIN extensible_8_data 
     ON records.instance_id = extensible_8_data.instance_id 
LEFT JOIN cloud_uploads 
     ON extensible_8_data.field_953 = cloud_uploads.id 
LEFT JOIN form_1_data 
     ON records.field_89 = form_1_data.instance_id 
WHERE instances.form_id = 6 
     AND instances.active = 1 
     AND (
       cloud_uploads.active = 1 
       OR cloud_uploads.active IS NULL 
       ) 
GROUP BY records.instance_id 
ORDER BY records.instance_id DESC LIMIT 0, 25 
+0

什麼是確切的標準「需要上傳的文件「位?一旦確定了醫生位數的限制應該更容易。 – BugFinder

+0

您的問題根據醫生和檔案分類。您的字段似乎不完全符合此術語。你能用與查詢一致的語言更清楚地表達你想要的內容嗎? –

+0

對不起:)記錄別名是爲醫生和cloud_uploads是文件。這些是在用戶創建的表單和表格上動態生成的查詢。 extensible_8_data.field_953包含用於將醫生數據加入到cloud_uploads的cloud_uploads標識。 –

回答

1

加入與他們的文件選擇的25名醫生所關注的子查詢:

SELECT SQL_CALC_FOUND_ROWS records.instance_id, 
     instances.patient_id, 
     TRIM(form_1_data.field_2) AS field_0, 
     TRIM(form_1_data.field_1) AS field_1, 
     TRIM(form_1_data.field_87) AS field_2, 
     GROUP_CONCAT(DISTINCT CONCAT (
         extensible_8_data.field_953, 
         '|||', 
         cloud_uploads.NAME 
         ) SEPARATOR '<br />') AS field_3 
FROM (SELECT * FROM form_6_data ORDER BY instance_id DESC LIMIT 25) AS records 
LEFT JOIN instances 
     ON instances.id = records.instance_id 
LEFT JOIN extensible_8_data 
     ON records.instance_id = extensible_8_data.instance_id 
LEFT JOIN cloud_uploads 
     ON extensible_8_data.field_953 = cloud_uploads.id 
LEFT JOIN form_1_data 
     ON records.field_89 = form_1_data.instance_id 
WHERE instances.form_id = 6 
     AND instances.active = 1 
     AND (
       cloud_uploads.active = 1 
       OR cloud_uploads.active IS NULL 
       ) 
GROUP BY records.instance_id 
ORDER BY records.instance_id