See More See More - 4 months ago 10
SQL Question

Select rows in a table where id is equals to another id in another table

I would like to select a certain row in my getadvocacy table where id is the id in another table called swimsuit.

swimsuit table

id | name | average
1 | Abc | 90
3 | Def | 99


getadvocacy

id | all_scores | average_score
1 | 70,70,70 | 70
2 | 70,70,70 | 70
3 | 70,70,70 | 70


Now, I want to select from getadvocacy but only 1 and 3 because it is the data on swimsuit.

Expected Output

id | all_scores | average_score
1 | 70,70,70 | 70
3 | 70,70,70 | 70


I tried this but it has different output.

select getadvocacy.id, all_scores, average_score from getadvocacy WHERE getadvocacy.id IN (select id from swimsuit)

Answer

If the id (primary key) is same for table then you can use join on Id

 select * from table1
        JOIN table2 
             on table1.id = table2.id

Use this

  select * from swimsuit JOIN getadvocacy ON swimsuit.id= getadvocacy.id;

Result of query is

    1   abc 90  1   50,60,70    70
    3   def 99  3   60,70,70    70
Comments