需要帮助加入两张桌子

时间:2011-10-04 06:52:57

标签: php sql join phpmyadmin

我一直无法理解如何将2个表连接在一起,因此我可以从一个表中获取信息并按另一个表进行排序。它实际上看起来像第一个表实际上创建了一个“行”变量以供稍后使用。我之前尝试过询问,但它没有帮助,所以我试着更好地解释,也许我会学到一些东西......我希望有人能够帮助我制作一个能够为我做这个的工作代码。我目前使用的代码是

$sql3 = new db;
$sql3->db_Select(DB_TABLE_ROSTER_TEAM_MEMBERS, "*", "team_id = ".intval($row1['team_id'])." ORDER BY member_team_order");

但是我需要通过另一个表上的diff字段来订购。在另一张桌子上,它看起来像这样

$sql3 = new db;
$sql3->db_Select(DB_TABLE_ROSTER_MEMBERS, "*", ORDER BY member_status");

请注意,我需要的另一张表是由member_status订购。

我猜这是可能的,但是join命令看起来非常复杂,我不能单独围绕它。所以我正在寻求专家的帮助(你)。 我尽力解释但如果有任何信息我可以提供帮助请告诉我。

这个代码我试图修改是在我之上,但我确实喜欢挑战...

表结构 -

表名 - roster_team_members
字段 - member_id,member_name,team_id,team_name,game_id,game_name,member_team_status,text_color,member_team_order

表名 - roster_members
字段 - member_id,昵称,real_name,external_image,active_external_image,member_status,leader_status,leader_order,member_application_date

UPDATE -

$sql3 = new db;
$sql3  ="select t.* from #roster_team_members as t ";
$sql3 .="inner join #roster_members on t.member_id =m.member_id ";
$sql3 .="where t.team_id=".intval($row1['team_id']);
$sql3 .="order by m.member_status";
$db->db_select_gen($sql3); 
while($row3 = $db->db_Fetch()) {

我得到致命错误 - 致命错误:在第523行的D:\ wamp \ www \ DTK_Testing_Site \ e107_plugins \ jbroster_menu \ jbroster.php中的非对象上调用成员函数db_select_gen()

第523行是$ db-> db_select_gen($ sql3);线。任何想法?

UPDATE2- 用这个替换了上面的内容 -

$sql3 = new db;
$sql3 -> db_Select_gen("select t.* from ".e107_jbclan_roster_team_members." as t 
inner join ".e107_jbclan_roster_members." on t.member_id =m.member_id 
WHERE t.team_id=@team_id
ORDER BY m.member_status") or die(mysql_error()); 

现在我只是得到这些小错误...'on clause'中的未知列'm.member_id' 我很接近完成这个。任何更多的帮助都会很棒...

while($row3 = $db->db_Fetch()) {

IV DONE IT !!!! 没有你Guilhem Hoffmann,我无法做到。非常感谢...最后这个问题得到了解决,我可以再次过上我的生活。 thx stackoverflow.com !!!!

1 个答案:

答案 0 :(得分:0)


您希望在两个表之间进行连接,例如

select t.*
from roster_team_members as t, roster_members a m
-- you grab the member with liaising team record and member one
where t.member_id =m.member_id and t.team_id = @team_id
-- you use the member status in member table to do the sort
order by m.member_status

使用内部语法

select t.*
from roster_team_members as t
-- you grab the member with liaising team record and member 
inner join roster_members on t.member_id =m.member_id
where t.team_id=@team_id
-- you use the member status in member table to do the sort
order by m.member_status

在Php中

 <?php
      // in the doc, you can find that # is used to select the current database scheme
      $sql  ="select t.* from #roster_team_members as t ";
      $sql .="inner join #roster_members on t.member_id =m.member_id ";
      $sql .="where t.team_id=".intval($row1['team_id']);
      $sql .="order by m.member_status";
      $db->db_select_gen($sql);