如何选择列值等于已知行值的行?

时间:2022-09-27 07:56:53

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.

如何选择链接到用户的行,具有特定ID的行属于该行。我希望能够查看行,按ID选择消息并选择链接到同一用户的所有行。

    select * from table1
        where user_id = -- the same as of the row with id = 3 for example

2 个解决方案

#1


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)

#2


not sure what sql this is but in SQL Server:

不确定这是什么SQL,但在SQL Server中:

select * from table1
where user_id = (select user_id from table1 where id = 3)

#1


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)

#2


not sure what sql this is but in SQL Server:

不确定这是什么SQL,但在SQL Server中:

select * from table1
where user_id = (select user_id from table1 where id = 3)