使用$ query = mysql_query

时间:2015-05-21 11:54:04

标签: php mysql database select

我可以使用此选择更多表吗?

$table = "users";
$query = mysql_query("SELECT title, smalltitle, FROM $table ORDER BY date DESC");
while($result= mysql_fetch_array($query))
{  
echo'<div id="title"> ';
    echo'<p>'.$result['title'].'</p>';
echo'</div>';

echo'<div id="name"> ';
    echo'<p>'.$result['name'].'</p>';
echo'</div>';
}

我希望表格“用户”中的title / smalltitle和表格“gmsg”中的名称/文字

3 个答案:

答案 0 :(得分:1)

使用此查询 -

"SELECT users.title, users.smalltitle, gmsg.name FROM users, gmsg $where_condition ORDER BY users.date DESC"

$where_condition将成为匹配users&amp;的条件。 gmsg喜欢 -

$where_condition = "users.id = gmsg.user_id"; //Or whatever it is

答案 1 :(得分:0)

是的,加入。在Mysql here中使用JOIN的文档。

示例:

SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
             ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

答案 2 :(得分:0)

$table = "users";
$table2 = "gmsg";
$query = mysql_query("SELECT a.title, a.smalltitle, b.name, b.text FROM $table a, $table2 b ORDER BY a.date DESC");