0

I have 2 tables.

Table Matches

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

Table Payments

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

Then in one query I want to get TeamA, TeamB, User and Quantity if they have same id.

I've tried this but it fails.

SELECT TeamA, TeamB FROM Matches WHERE id IN (SELECT idMatch, TeamA, TeamB FROM Payments)
3
  • please describe more as this is not clear your requirement. Commented Apr 26, 2013 at 11:44
  • How would your desired output look, given the example above? Commented Apr 26, 2013 at 11:44
  • please post your expected output Commented Apr 26, 2013 at 11:53

6 Answers 6

2

try this

 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

Sign up to request clarification or add additional context in comments.

Comments

2

you can use joins

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

Comments

2

try this

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

Comments

1

IN() should contain a list of values like IN('red','green','blue') or IN(1,3,53). SELECT is fine, but it has to return a single field. This would work.

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

However it looks like you want to achieve something you need JOIN or GROUP BY for.

Comments

1

Try

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.