简体   繁体   English

一个SQL查询的结果集可以在另一个SQL查询中使用吗?

[英]Can the result set of one sql query be used in another?

I have two tables in mySQL db (photoentry and member). 我在mySQL db中有两个表(photoentry和member)。 When the user logs in, I retrieve all there user details from the member table based on the email they used to login. 当用户登录时,我会根据他们用于登录的电子邮件从成员表中检索所有在那里的用户详细信息。

String sql = "SELECT * FROM member WHERE email='"+email+"'";

and displayed the results like so: 并显示如下结果:

 <td><b>Member ID: </b></td><td><input type="text" name="id" value="<%=resultSet.getString("id")%>"/> ></td> 

I want to use this member ID to retrieve the photoentry associated with the user. 我想使用此成员ID检索与用户关联的photoentry。 I have tried 我努力了

String sql = "SELECT * FROM photoentry p where (SELECT id from member m WHERE p.m_ID = m.id AND email='"+email+"'";

How can I retrieve the photoentry associated with the member using the result set of the original query? 如何使用原始查询的结果集检索与成员相关联的photoentry? (I am using JSP) (我正在使用JSP)

Please try this. 请尝试这个。

String sql = "
SELECT p.* FROM photoentry AS p
   INNER JOIN member AS m ON 
   p.member_id = m.id
        WHERE m.email LIKE '"+email+"'";

You can do: 你可以做:

String sql = "
    SELECT * FROM photoentry AS p
        JOIN member AS m ON (p.member_id = m.id)
            WHERE m.email LIKE '"+email+"'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM