-1

我有两张桌子

Create TABLE Comment(
    CommentText TEXT,
    CommentDate DATE NOT NULL,
    Time time,
    PostedBy VARCHAR(30),
    FOREIGN KEY (PostedBy) REFERENCES Employee(Name)
);

Create TABLE Employee(
 Name VARCHAR(30),
 Title VARCHAR(30),
 Onshift BOOLEAN,
 PRIMARY KEY(Name)
);

现在我该怎么做以下

$Comments =     mysql_query("SELECT * FROM Comment");
$all_comments = array();
while($row = mysql_fetch_array($Comments)) {
    $all_comments[] = $row;
}

foreach($all_comments as $commentrow){
    if($commentrow['PostedBy']){ //Check if The Employee is Onshift 
        Echo("The employee was on shift");
    }
    else{
        Echo("The employee was on shift");
    }
}

给定外键(postedby)的 IE 在该外键表中查找另一个值(在这种情况下,如果员工正在轮班,则为布尔值?)

4

2 回答 2

0

您的选择应该类似于

SELECT c.*, e.Name, e.Onshift FROM Comment as c join Employee as e on c.PostedBy = e.Name

然后你可以使用

$commentrow['Onshift']
于 2013-05-10T14:45:59.460 回答
0

您可以使用此评论任何 sql 并测试您的代码

//Priority on comment table 

$sql = "SELECT cmd.*, emp.* FROM Comment AS cmd
LEFT JOIN employee AS emp ON cmd.name = emp.name";

//use anyone of thsi based on ur need

$sql = "SELECT cmd.*, emp.* FROM Comment AS cmd
LEFT JOIN employee AS emp ON emp.name = cmd.name";

$Comments =     mysql_query($sql);
$all_comments = array();
while($row = mysql_fetch_array($Comments)) {

 if($row['PostedBy']){ //Check if The Employee is Onshift 
    echo("The employee was on shift");
 }
 else{
   echo("The employee was not in shift");
 }
}
于 2013-05-10T14:46:01.640 回答