我有两个查询,A和B,查询B只能运行,如果我得到查询A的结果,但现在我分别做两个查询。以下是我的查询:
查询
SELECT id, u_name, u_email, u_factory_id FROM wla_user WHERE email = 'john@gmail.com'
<表类>id u_name u_email u_factory_id tbody><<tr>1 约翰鸟 john@gmail.com 8 表类>
使用联合查询:
SELECT id, u_name, u_email, u_factory_id
FROM wla_user
WHERE email = 'john@gmail.com'
UNION ALL
SELECT id, door_details, con_details, factory_id
FROM wla_container
WHERE factory_id = $u_factory_id;
根据MySQL文档,当您需要查询a的结果来运行查询B时,您可以使用嵌套查询,如下所示:
查询
SELECT id, door_details, con_details, factory_id
FROM wla_container
WHERE factory_id IN (
SELECT u_factory_id
FROM wla_user
WHERE u_email = 'john@gmail.com'
);
<表类>id door_details con_details factory_id tbody><<tr>1 2 Tan 汽车零配件 8 23 Tan 摩托车零配件 8 3 3 Tan 厨房 8 表类>