0

我有 4 张桌子

Tbl_Items: 
   Asset Id, 
   Asset_name, 
   Workshop,
   PAT_PASS

(workshop 和 PAT_PASS 是 YES 或 NO。Workshop = YES 和 PAT_PASS = NO 表示该项目不可租用..)

Tbl_Project_details:
   Project_id, 
   Project_start_date, 
   project_end_date

TBL_Project_items:
   Project_id, 
   Asset_id,
   Start_date,
   End_date

我需要做一个选择语句,允许我显示 tbl_Items 中的所有项目,除了:

1) Those that are already in use on another project on the dates that I need them.
2) Are in the workshop. (Workshop = YES)
3) Are not PAT Tested (PAT_PASS = NO)

到目前为止,我已经从这样一个简单的声明开始:

$post_project_id = "13-1309.01";  //this is just an example number..

$sql = 
"SELECT * FROM Project_details 
Where Project_id = '$post_project_id' ";  
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) 
{
$hire_start_date = $row['Start_date']; // we find out our projects start date
$hire_end_date = $row['End_date'];     // we find out our projects end date
}  

$result=mysql_query("SELECT Items.Asset_id, Items.Name  
FROM 
Items,Project_items 
WHERE
Project_details.Start_date < '$hire_start_date' 
AND 
Project_details.End_date > '$hire_end_date'") 
or die(mysql_error());

while($row = mysql_fetch_array($result))
{
// fill up an option box with the results
}

即使这个基本脚本似乎也不起作用!这是我第一次尝试从多个表中进行选择,所以这是一个陡峭的学习曲线!

谢谢!

4

3 回答 3

0

我会使用子查询:

SELECT * FROM Tbl_Items AS i WHERE

这应该与您的 (1.) 条件相匹配:“仅获取那些使用该选择不会发生任何事件的记录”:

0 = (
SELECT COUNT(1) FROM TBL_Project_items AS pi
WHERE i.Asset_Id = pi.Asset_Id
AND Start_date < $hire_start_date
AND End_date > $hire_end_date
)

车间条件(2.):

AND WORKSHOP != "YES"

pat 测试条件 (3):

AND PAT_PASS != "NO"
于 2013-08-06T13:59:14.980 回答
0

$StartDate并且$EndDate是您的日期限制:

 SELECT ITEM.asset_id,
        ITEM.asset_name,
        ITEM.workshop,
        ITEM.pat_pass
   FROM Tbl_Items ITEM
        LEFT JOIN Tbl_Project_Items PJITEM
           ON PJITEM.asset_id = ITEM.asset_id
           AND PJITEM.start_date BETWEEN $StartDate AND $EndDate
           OR PJITEM.end_date BETWEEN $StartDate AND $EndDate
   WHERE ITEM.workshop = 'no'
     AND ITEM.pat_pass = 'yes' 
     AND PJITEM.asset_id IS NULL

帕特是谁?

于 2013-08-06T13:58:34.313 回答
-1

一个问题:

WHERE
Project_details.Start_date < '$hire_start_date' 
AND 
Project_details.End_date > '$hire_end_date'") 

“Start_date”和“End_date”列不在“Project_details”表中。在该表中,这些列似乎被命名为“Project_start_date”和“Project_end_date”。

于 2013-08-06T13:46:59.303 回答