简体   繁体   English

从mysql中的表中获取必须在另一个表中具有相应记录的数据

[英]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. 我想显示来自表名station的记录,其中station在歌曲表中至少有一首歌曲。 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. 请向我建议一种查询方式,向我显示在歌曲表中有歌曲的电台数据。请指定一种不返回记录且歌曲数为零的记录的方式。

What you're looking for is a INNER JOIN . 您正在寻找的是INNER JOIN You could join your stations table with your songs table by stations.station_id and songs.station_id . 您可以通过stations.station_idsongs.station_id将stations表与歌曲表songs.station_id This will work because INNER JOIN only return rows for which the join-predicate is satisfied. 这将起作用,因为INNER JOIN仅返回满足连接谓词的行。

I've made an example available at SQL Fiddle , but I do recommend spending a few minutes understanding mechanics of JOIN . 我已经在SQL Fiddle上提供了一个示例,但我建议您花一些时间来了解JOIN的机制

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

You could join the tables together at station_id . 您可以在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... 意思是这些ID( station_id )相等时,该电台会播放这首歌...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM