Home > Software design >  Select records from a table where other table row > 0
Select records from a table where other table row > 0

Time:11-17

I have the following two tables:

Table Animes with columns:
Id, Name

Table Episodes with columns:
Id, Episode Number, Anime

I need to select all rows in Table "Animes" with limit 10, where have at least 1 row on Table "Episodes" with Anime = Name

In other words i need to select animes if they have at least 1 episode..

Could someone help me please? Thanks!!

CodePudding user response:

you could use a select distinct for matching anime

select distinct id, name 
from animes.name 
inner join Episodes on animes.name = Episodes.Anime
limit 10

CodePudding user response:

You can use where exists pattern:

   select id, name 
   from Animes
   where exists (select 1 from Episodes where Animes.name = Episodes.Anime)
   limit 10;

SQL fiddle

  • Related