$sql = "SELECT * FROM absence
WHERE employee_id = '".$employee_id."' and
'".$startDate."' BETWEEN stdate and endate and '".$endDate."' BETWEEN stdate and endate ";
<?
$inputStart = "2012-06-02";
$inputEnd = "2012-06-05";
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
((endate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."')) ";
?>
SELECT * FROM absence WHERE (employee_id = '999001') and ((stdate BETWEEN '2012-06-12' and '2012-06-13') or (endate BETWEEN '2012-06-12' and '2012-06-13'))
$sql = "SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ('".$startDate."' BETWEEN stdate and endate) and ('".$endDate."' BETWEEN stdate and endate) ) ";
$sql = "SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ( ('".$startDate."' BETWEEN stdate and endate) or '".$startDate."' >= endate ) and ('".$endDate."' BETWEEN stdate and endate) ) ";
$sql = "SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ('".$startDate."' BETWEEN stdate and endate) and ('".$endDate."' BETWEEN stdate and endate) ) ";
น่าจะช้ OR น่ะครับ
Code
$sql = "SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ('".$startDate."' BETWEEN stdate and endate) or ('".$endDate."' BETWEEN stdate and endate) ) ";
<?
$inputStart = "2012-06-02";
$inputEnd = "2012-06-05";
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
((endate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."')) ";
?>
$sql = "SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ( ('".$startDate."' BETWEEN stdate and endate) or '".$startDate."' >= endate ) and ('".$endDate."' BETWEEN stdate and endate) ) ";
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
((endate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."')) ";
?>
แก้เป็น
Code (PHP)
<?
$inputStart = "2012-06-02";
$inputEnd = "2012-06-05";
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
((stdate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."')) ";
?>
SELECT * FROM absence WHERE (employee_id = '999001') and ((stdate BETWEEN '2012-06-12' and '2012-06-13') or (endate BETWEEN '2012-06-12' and '2012-06-13'))
ประเด็นอยู่ที่ว่า ถ้าใช้ and มันก็จะกำหนดช่วงเวลาที่แน่นอน
แต่ถ้าใช้ OR มันก็ได้ไม่ตรง
ผมเลยคิดว่า ใช้ and นี่แหล่ะ แต่กำหนดเงื่อนไขเพิ่มเติม ในการเช็ควันเริ่มต้น
ว่าการลาครั้งใหม่ให้สามารถไปซ้ำกับวันลา วันสุดท้ายของการลาครั้งก่อนได้
เลยขอเพิ่ม
.... employee_id = '".$employee_id."' and ( ( ('".$startDate."' BETWEEN stdate and endate) or '".$startDate."' >= endate ) and ('".$endDate."' BETWEEN stdate and endate) ) ";
<?
$inputStart = "2012-06-02";
$inputEnd = "2012-06-05";
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
(('".$inputStart."' BETWEEN stdate and endate) or ('".$inputEnd."' BETWEEN stdate and endate)) ";
?>
ตอนที่มัน where ตามพนักงานคนอื่น หรือว่าเป็นเพราะผม select แบบไม่ได้ใส่วงเล็บครับ
ตอนแรกผมใช้แบบนี้อะครับ
Code (PHP)
$sql = "SELECT * FROM absence
WHERE employee_id = '".$employee_id."' and
'".$startDate."' BETWEEN stdate and endate or '".$endDate."' BETWEEN stdate and endate ";
<?
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and
(('".$inputStart."' BETWEEN stdate and endate) or ('".$inputEnd."' BETWEEN stdate and endate)) ";
?>
มันจะเข้าเงื่อนไข
5/6/55 BETWEEN (stdate)1/6/55 - (endate)6/6/55
ใน Statement2 ว่า ('".$inputStart."' BETWEEN stdate and endate)
SELECT * FROM absence WHERE employee_id = '".$employee_id."' and ( ('".$startDate."' BETWEEN stdate and endate) and ('".$endDate."' BETWEEN stdate and endate) )
<?
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and (((('".$inputStart."' BETWEEN stdate and endate) or ('".$inputEnd."' BETWEEN stdate and endate)) ) OR (((stdate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."'))))
";
?>
<?
$sql = "SELECT * FROM absence
WHERE (employee_id = '".$employee_id."') and (((('".$inputStart."' BETWEEN stdate and endate) or ('".$inputEnd."' BETWEEN stdate and endate)) ) OR (((stdate BETWEEN '".$inputStart."' and '".$inputEnd."') or (endate BETWEEN '".$inputStart."' and '".$inputEnd."'))))
";
?>