加入同一个表和过滤器的两个SQL select查询

时间:2018-05-25 14:32:46

标签: php

我有一个医生表,下面列出了以下列,我想使用提交按钮按日期获取详细信息。但是我无法过滤查询以获取同一个表的详细信息。我将日期转换为特定日期并将其存储在$day变量中,并将id存储在$available变量中,方法是从上一页传输会话变量的值并在两个SQL查询中实现它。有人可以指导我吗?我很抱歉打字错误。

医生:

  id  doctorname    date1       day1 day2 day3 day4 day5 day6 day7
  1    arvind     2018-05-29   
  2    sonal      0000-00-00    mon   tue  wed  thu  fri  sat  sun
  3    harry      0000-00-00    mon   tue  wed  thu 

 <?php
   session_start();
   include 'connect.php';
   if(isset($_GET['submit'] ))
   { $date=$_GET['date'];
     $available= $_SESSION['available'];  
     $day=date('l', strtotime($date));

   $sql=mysqli_query($mysqli,"SELECT id,specilization,doctorName, 
   docFees, starttime, endtime,date1 FROM doctors WHERE 
  id= '$available' AND cast(date1 as date) = '$date'"  )
  UNION
  SELECT id,specilization,doctorName, docFees, starttime, endtime,date1 
  FROM doctors WHERE id= '$available' AND DAY1='$day' OR DAY2='$day' AND
  DAY3='$day' OR DAY4='$day' AND  DAY5='$day' OR DAY6='$day' OR
  DAY7='$day'");
  $row = mysqli_fetch_array($sql);
  If(empty($_GET['date'])) 
  {  echo "ENTER DATE  PlEASE";}   
  elseif($row1>1) 
   {echo "Day:". $day;
     echo "<br>";
     ?>  <tr>
         <td><?php echo $row1['id'];?></td>
            <td><?php echo $row1['specilization'];?></td>
            <td><?php echo $row1['doctorName'];?></td>
            <td><?php echo $row1['docFees'];?></td>
             <td><?php echo $row1['starttime'];?></td>
              <td><?php echo $row1['endtime'];?></td>
              <td><?php echo $_SESSION['bdate'];?></td>

        </tr>
       </table>
       <?php }?>

1 个答案:

答案 0 :(得分:0)

您不需要UNION,只需使用OR

$sql=mysqli_query($mysqli,"
    SELECT id,specilization,doctorName, docFees, starttime, endtime,date1 
    FROM doctors 
    WHERE id = '$available' 
    AND (cast(date1 as date) = '$date'
        OR '$day' IN (day1, day2, day3, day4, day5, day6, day7))");

DEMO