Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select rows that have a column value equal to the value of the known row?

Tags:

sql

There is a table:

    create table table1 (
        id integer primary key,
        user_id varchar(36),
        field1 varchar(100))

How do I select the rows linked to the user, to which the row with a specific id belongs. I'd like to be able to look at the rows, choose a message by id and select all the rows, linked to the same user.

    select * from table1
        where user_id = -- the same as of the row with id = 3 for example
like image 460
Alex Avatar asked Dec 14 '22 04:12

Alex


1 Answers

This is very easy with subqueries, in particular Comparisons Using Subqueries in the documentation:

SELECT * FROM table1 WHERE user_id = (SELECT user_id FROM table1 WHERE id = 3)
like image 71
Paolo Bergantino Avatar answered May 13 '23 21:05

Paolo Bergantino