mySQL - 具有多个字段的子查询

时间:2013-04-26 11:40:57

标签: mysql

我有2张桌子。

匹配

id | TeamA    | TeamB
--------------
1  | Barça    | Madrid
2  | Valencia | Depotivo

付款

idMatch | User  | Quantity
---------------------------
1       | Me    | 50
2       | Me    | 100

然后在一个查询中,我想获得TeamA,TeamB,用户和数量,如果他们有相同的ID。

我试过了,但失败了

SELECT TeamA, TeamB FROM Matches WHERE id IN (SELECT idMatch, TeamA, TeamB FROM Payments)

6 个答案:

答案 0 :(得分:2)

试试这个

 SELECT TeamA, TeamB ,User  , Quantity FROM Matches m
 inner join Payments p
 on p.idMatch = m.id
 WHERE id IN 
 (SELECT idMatch FROM Payments)

DEMO HERE

答案 1 :(得分:2)

你可以使用连接

select Matches.TeamA,Matches.TeamB,Payments.User,Payments.Quantity from Matches,Payments where Matches.id=Payments.idMatch

答案 2 :(得分:2)

试试这个

SELECT payments.user,
    payments.quantity
FROM
     Payments
     INNER JOIN Matches ON (Matches.id = Payments.idMatch);

答案 3 :(得分:1)

IN()应包含IN('red','green','blue')IN(1,3,53)等值的列表。 SELECT很好,但它必须返回一个字段。这样可行。

SELECT TeamA, TeamB FROM Matches
WHERE id IN (
   SELECT idMatch FROM Payments
)

但是看起来你想要实现你需要JOIN或GROUP BY的东西。

答案 4 :(得分:1)

尝试

SELECT 
    TeamA, 
    TeamB,
    payments.user,
    payments.quantity
FROM 
    matches
    JOIN payments ON ( matches.id = payments.matchid )

答案 5 :(得分:0)

select m.TeamA,m.TeamB,p.`User`,p.Quantity from matches m INNER join
payments p where p.idMatch=m.id