2012-03-25 58 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;