How to select rows with a column value equal to a known row value?

There is a table:

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

How to select rows associated with a user to which a row with a specific identifier belongs. I would like to be able to look at the lines, select the message by id and select all the lines associated with the same user.

    select * from table1
        where user_id = -- the same as of the row with id = 3 for example
+3
source share
2 answers

This is very easy with subqueries , in particular Comparison using Subqueries in the documentation:

SELECT * FROM table1 WHERE user_id = (SELECT user_id FROM table1 WHERE id = 3)
+10
source

not sure what sql is, but in SQL Server:

select * from table1
where user_id = (select user_id from table1 where id = 3)
+1
source

Source: https://habr.com/ru/post/1707637/


All Articles