2013-01-10 76 views
0

將一組電子郵件地址和數字放在一起,這些電子郵件地址和數字在使用連接的sql中從數據庫中取出。將字符串組合在一起並計算總數

SELECT ost_ticket.email, ost_ticket_response.time_taken 
FROM ost_ticket 
INNER JOIN ost_ticket_response 
ON ost_ticket_response.ticket_id=ost_ticket.ticket_id 
WHERE time_taken > 0 

輸出

('[email protected]', 100) 
('[email protected]', 100) 
('[email protected]', 10) 
('[email protected]', 2) 
('[email protected]', 30L) 
('[email protected]', 20L) 
('[email protected]', 1234567L) 
('[email protected]', 2L) 
('[email protected]', 2L) 
('[email protected]', 30L) 
('[email protected]', 10L) 
('[email protected]', 40L) 

希望能夠把所有的電子郵件地址,並有總讓

[email protected] = 216 
[email protected] = 30 
[email protected] = 70 
[email protected] = 1234567 
[email protected] = 50 

是否有可能通過SQL做或輸出做到這一點在後蟒蛇?

+1

你可以只改變你的SQL查詢中使用SUM和GROUP BY ... –

+0

@喬恩除非你使用的是ORM 。 – Swiss

+0

@Swiss。 。 。該問題用SQL標記。 –

回答

3

當你從數據庫中提取數據,使用這樣的查詢:

select emailaddress, sum(number) as total 
from t 
group by emailaddress 
+0

由於是完美 'SELECT ost_ticket.email,總和(ost_ticket_response.time_taken) FROM ost_ticket INNER JOIN ost_ticket_response ON ost_ticket_response.ticket_id = ost_ticket.ticket_id WHERE TIME_TAKEN> 0 GROUP BY ost_ticket.email' – namit

相關問題