2011-01-25 75 views
0

标题有点特定但最好我可以在一行中解释。如何从一个表中提取数据并将其插入另一个表中的列?

我的问题是基本上,我有我的数据库中的表,我查询;

<?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>"; 

?> 

现在这一切都是好的,但滴行需要拉其通过从项目表暴徒在我的分贝放弃了所有项目的图像

所以就需要在电话会议上商品表中的名称与上述查询中的生物名称相同的行中的图像。

我试着添加另一个查询内但没有做任何事情,我不能想到别的。

谢谢。

试图用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表中放到怪物表的drop列中。

回答

0

你需要做的就是所谓的JOIN。它允许您在单个SQL SELECT查询中从多个表中提取数据。

例如,要得到作者的每本书,你可能会做

SELECT * from 
book INNER JOIN author ON book.authorId = author.ID 

更多信息连接:http://www.w3schools.com/sql/sql_join.asp

+0

感谢您的快速回复,试了一下,见上面,想我失去了一些东西 – Rath 2011-01-25 20:16:12

相关问题