在另一个查询中使用查询结果

时间:2016-08-18 09:48:53

标签: php sql

我有一个问题:

$sql1="SELECT column1 FROM table_A WHERE username LIKE '".$username."';"; 
// This may return multiple rows

现在,我需要在另一个中使用此查询的结果:

$sql2="select all from table_B where mycolumn equals to result we got above"

2 个答案:

答案 0 :(得分:1)

试试这个:

$sql = "select all from table_B where mycolumn in (SELECT column1 FROM table_A WHERE username LIKE'".$username."')";

答案 1 :(得分:0)

使用JOINS:

 $sql = "SELECT 
                table_B.* 
         FROM table_A 
         INNER JOIN table_B ON (table_A.column1 = table_B.mycolumn)
         WHERE table_A.username LIKE '".$username."'";