多查询和结果加入

时间:2013-11-09 10:14:13

标签: php mysql

require_once("./Connections/root.php");
session_start();
$user = $_SESSION['MM_Username'];
$q = mysql_query("SELECT id FROM performer WHERE username = '$user'");
$r = mysql_fetch_assoc($q);
$q1 = mysql_query("SELECT poster_id, wall_msg FROM performer_info WHERE id = '$r[id]'");
$r1 = mysql_fetch_assoc($q1);
$q2 = mysql_query("SELECT username FROM performer WHERE id = '$r1[poster_id]'");
$r2 = mysql_fetch_assoc($q2);

echo $r2['username'] . " : " . $r1['wall_msg'];

是否可以在单个查询和结果中使用所有这些查询?

我认为,我需要使用内连接,但我不知道它是如何做到的

$ _ SESSION ['MM_Username']是用户名,用户名

2 个答案:

答案 0 :(得分:0)

您可以使用

之类的查询
select wall_msg from performer_info where poster_id = (select id from performer where username = '$user')

您已经知道的用户名...并且可以从此查询的结果中获取wall_msg。

编辑:

 select p.username, t.wall_msg from performer p, (select poster_id, wall_msg from performer_info where id=(select id from performer where username= '$user'))t where p.id = t.posterid;

这仅在内部查询返回单个结果时才有效。希望它有所帮助。

答案 1 :(得分:0)

内部联接怎么样?

SELECT t.username , pi.wall_msg 

FROM performer t

INNER JOIN performer_info pi ON pi.id = t.id

WHERE t.username LIKE '%$user%'