Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
$sql = "SELECT min(id) as sid FROM table1 where ...."; $result = mysql_query($sql); $row = mysql_fetch_array($result); $sid = $row['sid'];
当 sql 查询没有返回值时,如何将 $sid 设置为 0
(这里table1中的id为int类型)
请帮我....
如果没有匹配的行,min() 函数将返回 NULL 值,因此使用coalesceorifnull()将其变为零:
coalesce
ifnull()
SELECT coalesce(min(id), 0) AS sid ... SELECT ifnull(min(id), 0) AS sid ...
尝试这个:
$num_rows = mysql_num_rows($result); if($num_rows == 0) { $sid = 0; }
mysql_num_rows返回查询的行数