Skip to content
Advertisement

How to select from table A only where a relationship between the table A row and a specific row in B exists? [closed]

lets say I have two MySQL tables, table A and table B. Each table has a primary key called id and a name column. I also have a third table, table C, that contains relationships between table A and table B. Each row contains two foreign keys called a_id and b_id, which, as you might expect, correspond to ids in tables A and B.

What I want to do is select a random set of 10 table A rows, but only select rows that have a relationship with specific entries in table B. I don’t know which entries I’m looking for ahead of time, and I will start with their names. The names will be provided via query parameters.

I understand I should probably start with this:

But I don’t know how to structure the where clause.

Advertisement

Answer

You need something like this:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement