如何合并多个select语句

时间:2011-07-20 07:41:59

标签: php mysql

以下查询:

$today = date('Y-m-d');
$sql = "SELECT * FROM table WHERE day1 = '$today' AND country = 'India' OR day2 = '$today' AND country = 'India' OR day3 = '$today' AND country = 'India' OR day4 = '$today' AND country = 'India' OR day5 = '$today' AND country = 'India' OR day6 = '$today' AND country = 'India' OR day7 = '$today' AND country = 'India' OR day8 = '$today' AND country = 'India' OR day9 = '$today' AND country = 'India' OR day10 = '$today' AND country = 'India'";
if($_POST!=""){
  $mydate = mysql_real_escape_string($_POST['datepicker']);
  if($mydate!=""){  
    $sql = "SELECT * FROM table WHERE day1 = '$mydate' AND country = 'India' OR day2 = '$mydate' AND country = 'India' OR day3 = '$mydate' AND country = 'India' OR day4 = '$mydate' AND country = 'India' OR day5 = '$mydate' AND country = 'India' OR day6 = '$mydate' AND country = 'India' OR day7 = '$mydate' AND country = 'India' OR day8 = '$mydate' AND country = 'India' OR day9 = '$mydate' AND country = 'India' OR day10 = '$mydate' AND country = 'India'";   
  }     
}
.......
........
echo"......
......
......

与datepicker相关联。将仅在同一页面中回显。想要在同一页面中添加以下select语句....如果用户点击“下一个10天程序”。

select * from table WHERE (day1 >= CURRENT_TIMESTAMP
   AND country = 'India'
   AND day1 <= CURRENT_TIMESTAMP + INTERVAL 10 DAY
   AND country = 'India')
OR
   (day2 >= CURRENT_TIMESTAMP
   AND country = 'India'
   AND day2 <= CURRENT_TIMESTAMP + INTERVAL 10 DAY
   AND country = 'India')

......till day10

1 个答案:

答案 0 :(得分:1)

SELECT * FROM table 
 WHERE (
            (day1 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day2 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day3 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day4 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day5 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day6 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day7 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day8 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day9 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
         OR (day10 BETWEEN NOW() AND NOW() + INTERVAL 10 DAY)
       )
   AND country = 'India'