简体   繁体   中英

Join two SQL select queries of same table and filter

I have a table of doctors with following columns given below and I am wanting to fetch details by date using the submit button. But I can't filter the queries for details of the same table. I am converting the date to specific day and storing it in $day variable and the id in $available variable through transferring the value of session variable from previous page and implementing it in two SQL queries. Can anyone please guide me? I am sorry for any typing mistakes.

Doctors:

  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 }?>

You don't need UNION , just use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM