如何使用单个查询列出查询行或显示“无记录" [英] How to list rows for a query or display 'no records' using a single query
问题描述
如何在单个查询中没有返回行的情况下列出查询记录并显示无记录"?
How to list records for a query and to display "no records" when no rows returned using a single query?
当前我正在使用COUNT(*)
查询或使用mysql_num_rows()
函数;然后另一个查询以不同的结果集列出数据.您能告诉我是否可以通过一个查询来完成相同的工作?
Currently I am using a COUNT(*)
query or using mysql_num_rows()
function; then another query in different result set to list data. Can you tell me if it is possible to accomplish the same with a single query?
推荐答案
重要提示::我假设OP使用PHP,因为他提到了mysql_num_rows
.我希望他能告诉我我是否错了.
Important: I assume that the OP uses PHP as (s)he mentions mysql_num_rows
. And I hope (s)he will tell me if I am wrong.
检查结果是否为空集是您在PHP中的工作.我不明白您为什么必须再进行一次查询.也许您必须澄清您的问题.
It is your job in PHP to check whether the result is an empty set or not. I don't understand why you have to do another query. Maybe you have to clarify your question.
这里有一个更完整的示例:
Here a more complete example:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
// If if result set contains rows
if(0 == mysql_num_rows($result)) {
echo 'no records';
}
else { // Loop over the result set
while(row = mysql_fetch_array($result)) {
// do whatever you want with the data here
}
}
参考: mysql_num_rows
, mysql_fetch_array
即使您不使用PHP,其他语言也是如此,并且应该有类似的功能.
Even if you don't use PHP, the approach is the same in other languages and there should be similar functions available.
这篇关于如何使用单个查询列出查询行或显示“无记录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!