[英]How do i pull data from one table and insert it into a column in another table?
the title is a little under specific but its best i could explain in one line. 标题有些特殊,但我可以在一行中解释其最佳含义。
my problem is basically, i have a table in my db which i query with; 我的问题基本上是,我在数据库中有一个查询表;
<?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>";
?>
now this is all fine, but the drops row needs to pull images of all the items which are dropped by that mob from the items table in my db 现在一切都很好,但是drops行需要从我的数据库中的items表中提取该暴民删除的所有项目的图像
so it would need to call on the items table for the images from the rows that have a dropped by name the same as the mob name from the above query. 因此,需要在项目表中调用名称与上述查询中的暴民名称相同的行中的图像。
i tried adding another query within the but that did nothing, i cant think of anything else. 我尝试在中添加另一个查询,但没有执行任何操作,我想不起其他任何事情。
thanks. 谢谢。
tried to mod my code using INNER JOIN to; 试图使用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>";
?>
but now this is only causing my mobs table to load each mob by the amount of items in my misc table, so i have mob1 100 times and mob2 100 times and so on, but it doesnt pull the images from the misc table and put them in the drops column in the mobs table. 但是现在这只会导致我的暴民表按我的杂项表中的项目数量加载每个暴民,所以我有mob1 100次和mob2 100次,依此类推,但是它没有从misc表中提取图像并将其放置在小怪兽表的drops列中。
What you need to do is called a JOIN. 您需要做的就是加入。 It allows you to pull data from multiple tables in a single SQL SELECT query. 它允许您在单个SQL SELECT查询中从多个表中提取数据。
For example, to get the authors for each book, you might do 例如,为了获得每本书的作者,你可能会做
SELECT * from
book INNER JOIN author ON book.authorId = author.ID
More information on JOINs: http://www.w3schools.com/sql/sql_join.asp 有关JOIN的更多信息: http : //www.w3schools.com/sql/sql_join.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.