2012-03-25 21 views
1

下面兩個表平局是我的表在MySQL:查詢在一個

 ----------------1------------------------- 
     +---------------------------+----------+ 
     | email      | attempts | 
     +---------------------------+----------+ 
     | [email protected]   |  70 | 
     | [email protected]    |  1 | 
     | [email protected]  |  1 | 
     | [email protected]   |  115 | 
     +---------------------------+----------+ 
     ---------------2------------------------ 
     +----------+---------------------------+ 
     | accesses | email      | 
     +----------+---------------------------+ 
     |  24 | [email protected]   | 
     |  0 | [email protected]    | 
     |  0 | [email protected]  | 
     |  90 | [email protected]   | 
     +----------+---------------------------+ 

查詢在一個

Email Accesses Attempts 
email1 24   70 
email2  0    1 
email3  0    1 
email4 90   115 

它是SQL兩個表抽獎:

  1. first - >select accesses, email from user;
  2. second - >select email,count(*) as intentos from userstats where intento =1 group by email;

我該怎麼做? 我需要一個查詢來繪製。感謝的提前..

+2

曾經heared約'join'? – 2012-03-25 07:23:16

+0

哪個是'userstats'表? – egrunin 2012-03-25 07:41:37

+0

你應該考慮規範你的模式。如果每個電子郵件地址在每個表中都有一個條目,那麼您可能需要考慮將這兩個表合併成一個。 – liquorvicar 2012-03-25 09:32:51

回答

1
SELECT table1.email as Email, 
    table2.accesses as Acesses, 
    table1.attempts as Attempts 
from table1 
INNER JOIN table2 
on table1.email = table2.email 
+0

注:去掉錯誤的逗號 – egrunin 2012-03-25 07:39:04

+0

感謝。我的查詢選擇userstats.email電子郵件,計數(intento)作爲intentos,user.accesses從userstats內logeos通過電子郵件join上user.email = userstats.email羣組用戶; – geronimo76 2012-03-25 08:24:30

0

您可以使用INNER JOIN做到這一點

SELECT 
    t1.email, t1.accesses , t2.attempts 
FROM table1 t1 
INNER JOIN 
    table2 t2 ON 
     t2.email = t1.email;