MySQL cast函数
MySQL CAST函数用于将一个类型(数据类型)的值突变为另一种类型。
换句话说,CAST()函数需要一个类型的值并产生另一种类型的值。
语法
cast (exp As Type)
"类型Type"可以具有以下值:
- 二进制
- char
- 日期
- 日期时间
- 十进制
- 带符号[整数]
- 时间
- 无符号 [整数]
在PHP中使用MySQL cast函数示例
在该示例中,CAST函数将"DateTime"数据类型转换为"date"数据类型。
<?php $con=mysql_connect("localhost","root",""); if (!$con) { die('无法连接mysql数据库: ' . mysql_error()); } mysql_select_db("mysql", $con); print "<h2>MySQL: Simple Select statement</h2>"; $result = mysql_query("select * from mcnemployee"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>EmpId</th> <th>EmpJoinDate</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['emp_id'] . "</td>"; echo "<td>" . $row['Fname'] . "</td>"; echo "<td>" . $row['Lname'] . "</td>"; echo "<td>" . $row['emp_JoinDate'] . "</td>"; echo "</tr>"; } echo "</table>"; print "<h2>MySQL: With Cast Function</h2>"; $result = mysql_query("select CAST(emp_JoinDate as Date) As JoinDate from mcnemployee;"); echo "<table border='1'> <tr> <th>EmpJoinDate</th> </tr>";+- while($row = mysql_fetch_array($result)) { echo "<td>" . $row['JoinDate'] . "</td>"; echo "</tr>"; } echo "</table>"; ?>
日期:2020-06-02 22:15:37 来源:oir作者:oir