简体   繁体   English

使用$ query = mysql_query选择更多表

[英]Select more tables using $query = mysql_query

Can I select more tables using this? 我可以使用此表选择更多表格吗?

$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>';
}

I want title/smalltitle from table "users" and name/text from table "gmsg" 我想要表“ users”中的标题/小标题和表“ gmsg”中的名称/文本

Use this query - 使用此查询-

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

$where_condition would be the condtion to match users & gmsg like - $where_condition将是匹配usersgmsg的条件,例如-

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

Yes, with JOIN. 是的,加入JOIN。 Documentation for use JOIN in Mysql here . 使用文件在MySQL JOIN 这里

Sample: 样品:

SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
             ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
$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");

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

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