我有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)
答案 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)
答案 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