Rella
Rella

Reputation: 66945

I have 2 MySql tables. How to get not all data from them in 1 sql query?

I have 2 MySql tables. users with id's and username's ; streams with userId's and streamId's How to get them as / join them into one table containing only username | streamId as SQL response? With one SQL query.

Upvotes: 3

Views: 100

Answers (3)

Nate Noonen
Nate Noonen

Reputation: 1371

select tb1.username, tb2.streamid 
from tb1
inner join tb2 on tb2.userid = tb1.userid

The response above returns the same results, just contains an implicit join which my be slower.

Upvotes: 2

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171421

select u.username, max(s.streamId) as streamId
from users u
inner join streams s on u.id = s.userId
group by u.username

Upvotes: 0

msemelman
msemelman

Reputation: 2937

you can do the following:

select a.username, b.streamId
from names a, streams b
where a.userId = b.userId;

Upvotes: 2

Related Questions