mayur bhagat
mayur bhagat

Reputation: 209

Fetch data from table in mysql that must have corresponding records in another table

I want to show records from table name station where station have at least one song in song table. Table structure

station
station_id
stration_name
station_description

song
song_id
station_id
song_location

Please suggest me the way to form query that shows me station data which have songs in song table.please specify a way that do not returns record with corresponding songs zero count.

Upvotes: 0

Views: 39

Answers (3)

Kevin Sjöberg
Kevin Sjöberg

Reputation: 2254

What you're looking for is a INNER JOIN. You could join your stations table with your songs table by stations.station_id and songs.station_id. This will work because INNER JOIN only return rows for which the join-predicate is satisfied.

I've made an example available at SQL Fiddle, but I do recommend spending a few minutes understanding mechanics of JOIN.

Upvotes: 1

ThomasK
ThomasK

Reputation: 2220

You could join the tables together at station_id.
It looks like each song is linked to a spesific station. Meaning where these ID (station_id) is equal, the station has this song...

Upvotes: 0

Strawberry
Strawberry

Reputation: 33945

SELECT DISTINCT something 
           FROM somewhere 
           JOIN somewhere_else 
             ON somewhere_else.other_thing = somewhere.thing;

Upvotes: 0

Related Questions