2014-01-08 72 views
0

我正在使用此查詢從3個表中獲取數據。使用加入運行查詢時結果增加一倍

SELECT sum(tu.filesize) AS totalSize, tu.sender, tu.receiver, tu.valid_time, group_concat(tu.filename) AS files, tu.file_path, us.uid temp_id, tu.unique_key, tu.uid user_id, tu.file_encryption, t_l.encryption_password 
FROM tbl_uploads tu 
LEFT JOIN tbl_users us ON us.id = tu.uid 
LEFT JOIN tbl_logs t_l ON t_l.u_key = tu.unique_key 
WHERE date(tu.valid_time) = '2014-01-09' 
AND tu.sender <> '' 
GROUP BY tu.unique_key 

,但沒有得到預期的結果,因爲tbl_uploadstbl_logs幾乎與名稱filesize, filename同場,所以當我運行此查詢我得到了雙重文件大小462804和文件;文件大小應爲231402

+1

我不認爲這是因爲在tbl_logs類似的領域。您可能會多次獲取tbl_uploads中的行,因爲tlb_logs中有多行與tbl_uploads中的一行有關。 – Rembunator

+0

@Rembunator yes在'tbl_logs'中也有多行,與'tbl_uploads'相同,但是在'tbl_uploads'中,我用group by做了單行的結果,但是由tbl_logs的組不行,以及我也嘗試過。像'GROUP BY tu.unique_key,t_l.u_key' –

回答

0

爲了避免重複行從tbl_logs嘗試:

SELECT sum(tu.filesize) AS totalSize, tu.sender, tu.receiver, tu.valid_time, group_concat(tu.filename) AS files, tu.file_path, us.uid temp_id, tu.unique_key, tu.uid user_id, tu.file_encryption, t_l.encryption_password 
FROM tbl_uploads tu 
    LEFT JOIN tbl_users us ON us.id = tu.uid 
    LEFT JOIN (SELECT u_key, encryption_password FROM tbl_logs GROUP BY u_key, encryption_password) AS t_l ON t_l.u_key = tu.unique_key 
WHERE date(tu.valid_time) = '2014-01-09' 
AND tu.sender <> '' 
GROUP BY tu.unique_key 
+0

你可以用分組的方式來查看tbl_logs並加入這個視圖。但是你會從日誌表中得到任意的行,這可能不是你想要的。它看起來像你可能需要重新考慮你的數據庫結構。如果任意行不是問題,你可能會在多行中有相同的數據,這對我來說似乎不是一個好主意。 – Rembunator