0

我有 2 张桌子。

匹配

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

餐桌付款

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

然后在一个查询中,我想获取 TeamA、TeamB、User 和 Quantity(如果它们具有相同的 ID)。

我已经尝试过了,但它失败了

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

6 回答 6

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)

在这里演示

于 2013-04-26T11:47:21.440 回答
2

你可以使用连接

select Matches.TeamA,Matches.TeamB,Payments.User,Payments.Quantity from Matches,Payments where Matches.id=Payments.idMatch
于 2013-04-26T11:48:25.760 回答
2

尝试这个

SELECT payments.user,
    payments.quantity
FROM
     Payments
     INNER JOIN Matches ON (Matches.id = Payments.idMatch);
于 2013-04-26T11:52:45.767 回答
1

IN() 应该包含一个值列表,例如IN('red','green','blue')or IN(1,3,53)。SELECT 很好,但它必须返回一个字段。这会奏效。

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

但是,您似乎想要实现需要 JOIN 或 GROUP BY 的目标。

于 2013-04-26T11:44:09.410 回答
1

尝试

SELECT 
    TeamA, 
    TeamB,
    payments.user,
    payments.quantity
FROM 
    matches
    JOIN payments ON ( matches.id = payments.matchid )
于 2013-04-26T11:47:06.737 回答
0
select m.TeamA,m.TeamB,p.`User`,p.Quantity from matches m INNER join
payments p where p.idMatch=m.id
于 2013-04-26T11:58:44.403 回答