[英]How do i pull data from one table and insert it into a column in another table?
標題有些特殊,但我可以在一行中解釋其最佳含義。
我的問題基本上是,我在數據庫中有一個查詢表;
<?php
include 'connect.php';
$query = mysql_query("SELECT * FROM mobs WHERE zone='Darnolth' ORDER BY lvl ") or die(mysql_error());;
echo "<table border='1' cellpadding='2' cellspacing='0' width=800 id='mob' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Mob Name</th> <th>Level</th> <th>Health</th> <th>Drops</th> <th>Effects</th> <th>Zone</th></tr></thead><tbody>";
while($row = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo $row['image'];
echo "</td><td width=200>";
echo $row['mobname'];
echo "</td><td width=50>";
echo $row['lvl'];
echo "</td><td width=50>";
echo $row['health'];
echo "</td><td width=200>";
echo $row['drops'];
echo "</td><td width=100>";
echo $row['effects'];
echo "</td><td width=75>";
echo $row['zone'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
現在一切都很好,但是drops行需要從我的數據庫中的items表中提取該暴民刪除的所有項目的圖像
因此,需要在項目表中調用名稱與上述查詢中的暴民名稱相同的行中的圖像。
我嘗試在中添加另一個查詢,但沒有執行任何操作,我想不起其他任何事情。
謝謝。
試圖使用INNER JOIN修改我的代碼;
<?php
include 'connect.php';
$query = mysql_query("SELECT mobs.image, mobs.mobname, mobs.lvl, mobs.health, mobs.drops, mobs.effects, mobs.zone, misc.droppedby FROM mobs JOIN misc ON mobs.drops = misc.droppedby") or die(mysql_error());;
echo "<table border='1' cellpadding='2' cellspacing='0' width=800 id='mob' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Mob Name</th> <th>Level</th> <th>Health</th> <th>Drops</th> <th>Effects</th> <th>Zone</th></tr></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['image'];
echo "</td><td width=200>";
echo $row['mobname'];
echo "</td><td width=50>";
echo $row['lvl'];
echo "</td><td width=50>";
echo $row['health'];
echo "</td><td width=200>";
echo $row['drops'];
echo "</td><td width=100>";
echo $row['effects'];
echo "</td><td width=75>";
echo $row['zone'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
但是現在這只會導致我的暴民表按我的雜項表中的項目數量加載每個暴民,所以我有mob1 100次和mob2 100次,依此類推,但是它沒有從misc表中提取圖像並將其放置在小怪獸表的drops列中。
您需要做的就是加入。 它允許您在單個SQL SELECT查詢中從多個表中提取數據。
例如,為了獲得每本書的作者,你可能會做
SELECT * from
book INNER JOIN author ON book.authorId = author.ID
有關JOIN的更多信息: http : //www.w3schools.com/sql/sql_join.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.