根据结果​​集中的值选择“查询”

时间:2014-06-10 06:26:57

标签: mysql database

无论如何,根据包含多个值的一个查询的结果从1个表中选择全部而不必进行2个单独的查询?

假设长连接查询将生成id

列表

SELECT xyz FROM table long join query WHERE id = array of ids found in result table

添加了示例:

SELECT * FROM tweets as t 
where t.user_id
in(
SELECT uff.id, uff.username 
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)

括号中的位返回用户关注者的ID和用户名(uff.id = 1)

如何通过生成的结果集中的所有id获取所有“推文”

4 个答案:

答案 0 :(得分:0)

我认为你的意思是

OP编辑后编辑

SELECT * FROM tweets as t 
WHERE t.user_id
in(
    SELECT uff.id   //remove the second field, you just need the id
    FROM users as uf
    LEFT JOIN followlinks as fl
    ON uf.id = fl.user_id
    LEFT JOIN users as uff
    ON fl.follow_id = uff.id
    WHERE uff.id = 1
)

答案 1 :(得分:0)

您可以使用子查询:

SELECT * FROM `table1` WHERE `id` IN (SELECT `table2`.id FROM `table2` )

您可能需要查看syntax

的文档

答案 2 :(得分:0)

SELECT xyz FROM table_A join table_B WHERE table_A.id IN (SELECT ID FROM table_C);

答案 3 :(得分:0)

在尝试了in子句后,我无法得到我所追求的结果,但在重新思考我想要做的事情后,我得到了一个额外的连接条款

SELECT uff.username, t.content
FROM users as uf
JOIN followlinks as fl
ON uf.id = fl.user_id
JOIN users as uff
ON fl.follow_id = uff.id
JOIN tweets as t
ON t.user_id = uff.id
WHERE uf.id = 1