结合mySQL语句

时间:2013-06-04 02:26:34

标签: php mysql

如何将所有这些语句组合成一个并根据需要在我的脚本中调整日期?

$sunday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(DATE_ADD(CURRENT_DATE, INTERVAL +7 DAY)) AND DAYOFWEEK(h.date) = 1
                                ORDER BY e.emp_id ASC");
$monday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(DATE_ADD(CURRENT_DATE, INTERVAL +7 DAY)) AND DAYOFWEEK(h.date) = 2
                                ORDER BY e.emp_id ASC");
$tuesday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(DATE_ADD(CURRENT_DATE, INTERVAL +7 DAY)) AND DAYOFWEEK(h.date) = 3
                                ORDER BY e.emp_id ASC");
$wednesday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(CURRENT_DATE) AND DAYOFWEEK(h.date) = 4
                                ORDER BY e.emp_id ASC");
$thursday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(CURRENT_DATE) AND DAYOFWEEK(h.date) = 5
                                ORDER BY e.emp_id ASC");
$friday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(CURRENT_DATE) AND DAYOFWEEK(h.date) = 6
                                ORDER BY e.emp_id ASC");
$saturday =  mysql_query("SELECT h.timestart, h.timeend, h.work, h.why_off, h.work_type, e.show FROM hours AS h, employees AS e 
                            WHERE h.s_id = '" .$s_id. "' AND e.u_id = h.u_id AND WEEK(h.date) = WEEK(CURRENT_DATE) AND DAYOFWEEK(h.date) = 7
                                ORDER BY e.emp_id ASC");

有一天的剧本就像我需要的那样。

<div class="hours">
    <?php $starting = date("g:ia", strtotime($time['timestart']));
        $ending = date("g:ia", strtotime($time['timeend']));
            if ($time['work'] == '1') {
                if ($time['why_off'] == '1') 
                    {echo "<div class='off'>OFF [R]</div>";}
                elseif ($time['why_off'] == '2') 
                    {echo "<div class='off'>OFF [ML]</div>";}
                elseif  ($time['why_off'] == '0')
                    {echo "<div class='off'>OFF</div>";}
            }
                elseif ($time['work'] == '0') {
                    if ($time['work_type'] == '1') {
                        if ($time['timeend'] == '23:00:00')
                        {echo "<div class='working_w'>".$starting." - Close<br>[TL]</div>";}
                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TL]</div>";}
                    }
                    elseif ($time['work_type'] == '2') {
                        if ($time['timeend'] == '23:00:00')
                        {echo "<div class='working_w'>".$starting." - Close<br>[SF]</div>";}
                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[SF]</div>";}
                    }
                    elseif ($time['work_type'] == '3') {
                        if ($time['timeend'] == '23:00:00')
                        {echo "<div class='working_w'>".$starting." - Close<br>[-/+]</div>";}
                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[-/+]</div>";}
                    }
                    elseif ($time['work_type'] == '4') {
                        if ($time['timeend'] == '23:00:00')
                        {echo "<div class='working_w'>".$starting." - Close<br>[TR]</div>";}
                            else {echo "<div class='working_w'>".$starting." - ".$ending."<br>[TR]</div>";}
                    }
                    elseif ($time['work_type'] == '0') {
                        if ($time['timeend'] == '23:00:00')
                        {echo "<div class='working'>".$starting." - Close</div>";}
                            else {echo "<div class='working'>".$starting." - ".$ending."</div>";}
                    }
                }?>

</div>
<?php } ?>

我希望将其全部整合在一起,以便我可以调用上一个和/或下一个链接来显示数据库中的其他几周。

提前致谢:)

1 个答案:

答案 0 :(得分:1)

未经测试

SELECT h.timestart, 
       h.timeend, 
       h.work, 
       h.why_off, 
       h.work_type, 
       e.show 
FROM
       hours AS h INNER JOIN employees AS e ON e.u_id = h.u_id
WHERE h.s_id = '{$s_id}' 
      AND WEEK(h.date) = WEEK(DATE_ADD(CURRENT_DATE, 
      INTERVAL + (CASE 
           WHEN DAYOFWEEK(h.date) BETWEEN 1 AND 3 THEN 7 
           ELSE 0 END
      ) DAY))
ORDER BY e.emp_id ASC