简体   繁体   English

我如何从一个表中提取数据,并将其插入到另一个表中的列?

[英]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.

相关问题 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? 如何将数据从一个表拉到另一个表 - How to pull data from one table into another 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 如何将数据从一个表动态插入到另一个表? - How should I insert data from one table to another dynamically? 如何在另一个表列中插入一个表数据 - How to insert one table data inside another table column 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 在PHP中,如何从一个表中获取信息以另一表的形式获取信息 - in PHP, how do I get info from one table to pull up in a form for another 如何在 php-mysql 中使用 pdo 从一台服务器连接(拉取数据)到(插入 - 创建表)另一台服务器,可能是获取? - how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM