2013-07-17 24 views
0

获取行我有2个表:从2个表

tblDowntime 
downtimeID - downtimeDescription 
1   - dt_desc 
2   - dt_desc2 


tblMachineArea 
areaID  - downtimeID - areaDescription 
1   - 1   - area1 
2   - 2   - area2 

我知道downtimeDescription,所以我有什么做的就是从tblMachineArea相应的领域?

我可以从ID得到他们,但我的列表框只会有

我有this answer为INSERT说明,但无法适应它...除非我失去了一些东西真的很明显..

我的另一个问题是什么联接使用...我试过内部联接与machineID = 1,它返回tblMachineArea中的所有行,但具有不同的机器ID取决于如果我在查询中使用1或2 .. ..对我没有多大意义

编辑:我跑的查询是

SELECT areaID, tblMachineArea.downtimeID, tblDowntime.downtimeDescription, areaDescription 
FROM tblDowntime 
INNER JOIN tblMachineArea ON tblMachineArea.downtimeID = 1; 

图像:(请原谅的表名和字段稍有不同,所以我已经更新了原件) enter image description here

任何帮助,将不胜感激

+0

向我们展示您尝试的查询。 –

+0

由于您的数据仅通过ID链接,因此您无法仅使用列表框获取相应记录。您必须从持有机器的列表框中进行描述,在您的tblMachine中搜索相关标识,然后在tblDowntime中搜索该标识的对应记录。 –

+0

内部连接应该是检索所需记录的正确方法。请显示您的查询。 –

回答

1

你可以尽量避免使用的连接和运行两个查询。

首先获取机器的ID并将其作为变量存储。 TH使用该变量来获取所需的信息。

此外,您提到的链接不使用准备语句,如果能够提高安全性,最好将其用作策略。

这是我注册用户的PHP函数,您可以看到预先准备的语句是如何构建的。

//function for registering a new user and adding their details to the database 
public function register($username, $password, $email){ 

$time  = time(); 
$ip   = $_SERVER['REMOTE_ADDR']; 
$emailCode = sha1($username + microtime()); 
$password = sha1($password); 

//prepare statement and bind values 
$query = $this->db->prepare("INSERT INTO `users` (`username`, `password`, `email`, `ip`, `time`, `emailCode`) VALUES (?, ?, ?, ?, ?, ?) "); 

$query->bindValue(1, $username); 
$query->bindValue(2, $password); 
$query->bindValue(3, $email); 
$query->bindValue(4, $ip); 
$query->bindValue(5, $time); 
$query->bindValue(6, $emailCode); 

try{ 
    $query->execute(); 

    //function stuff here removed to save space 

}catch(PDOException $e){ 
    die($e->getMessage()); 
} 
}//end register function 
+0

令人沮丧的是,这是我最终不得不做的,抱歉,迟到的答案接受 – iabbott

+0

没问题。很高兴你有了我 – null