CURDATE() 不适用于 SQL Server,我可以使用什么来替换


CURDATE() does not work for SQL Server what can I use in replace

我有一个简单的问题,我正在将PDO与我以前CURDATE()过的SQL Server一起使用,但这不起作用,因为SQL Server不支持它。只有MySQL可以,我添加了GETDATE()但这也不起作用。我如何能够获得当前日期。我希望信息仅按 id 和今天的日期显示名字顺序。

$stmt = $db->prepare("SELECT * FROM students WHERE name='Firstname' And Getdate() ORDER BY id DESC");
$stmt->execute()

这就是我的意思

 $stmt = $db->prepare("SELECT * FROM students WHERE name='BOB' And Getdate() ORDER BY id DESC");
 $stmt->execute()

整个查询页。

<!DOCTYPE> 
    <html>
    
    <head>
    
    <title></title>
    </head>
    <body>
    <?php
    //connects to the database
    require_once("../../db_connect.php");
    
    //prepared statement with PDO to query the database
    $stmt = $db->prepare("SELECT * FROM requests WHERE name='Bob' AND Date = CAST(GETDATE() AS DATE) ORDER BY id DESC");
    $stmt->execute();
    
    ?>
    
    <?php //start of the while loop ?>
    <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
    
 <table border="1">
    
    <br>
    <tr> 
        <th style="width:25px; height: 25px;">ID</th>
        <th style="width:90px; height: 25px;">Name</th>
        <th style="width:40px; height: 25px;">Date</th>
   
    </tr>
    <tr style="width:25px">
    <?php $id = $row['id'];?>
    <?php echo  "<td> <a href='../../update.php?id=$id'>$id</a></td>"?>
        <td style="width:90px; height: 12px;"><?php echo $row['name']; ?></td>
        <td style="width:40px; height: 12px;"><?php echo $row['date']; ?></td>
  
    </tr>
     
    </table>
    
      <?php } //end of the while loop?>
    </body>
</html>

这是用户更新信息的页面

<?php
    include('db_connect.php');
    $id=$_GET['id'];
    $result = $db->prepare("SELECT * FROM students WHERE id= :id");
    $result->bindParam(':id', $id);
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
?>
<html>
<head>
<title></title>

</head>
<body class='body'>
<form action = "update_process.php"  method ="post" class="Form">
        
<p><input type ="hidden" name = "id" value="<?php print($id); ?>"</p>
<table border='1' align="center">
<tr>    
    <td>Name:</td>
<td><input type="text" value ="<?php  print($row['name']) ?>"name="name"></td>
</tr>
<tr>    
    <td>Date</td>
<td style="width: 303px">
<input type="text" value ="<?php echo date("Y-m-d",time())?>"name="date" style="width: 148px"></td>

</tr>
</table>
    
<input type="submit" value= "Update Information">
<br>
</div>
</form>

</body>
</html>
<?php
    }
?>

SQL Server中没有等效的函数来匹配MySQL中的CURDATE(),但您可以通过将GETDATE()转换为DATE日期类型(去除时间部分)来模仿它:

CAST(GETDATE() AS DATE)
但是,您的

SQL语句仍然没有意义,我怀疑您的意思是将日期与表中的列进行比较,如下所示:

SELECT * 
FROM students 
WHERE name='Firstname' 
AND SomeDateColumn = CAST(GETDATE() AS DATE)
ORDER BY id DESC
相关文章: