Return Dummy Value If No Row Present for in Clause

Select dummy values if where clause returns zero rows

The problem is, that if the WHERE clause eliminates all rows, you end up with an empty set.

One way around it, is to use an aggregate function to force one row in the result.

Then, with COALESCE (or ISNULL), you can assign your default values.

This solution is only feasible if:

  • your basic query always returns exactly 0 or 1 row
  • your columns do not contain NULL
  • the selected columns can be aggregated (e.g. it will not work for text columns)
  • your default values have the same data type as the columns

For example:

select COALESCE(MAX(step_id),123456) AS step_id,
COALESCE(MAX(step_name),'There are no step_id matching the WHERE clause') AS step_name
from msdb.dbo.sysjobhistory
where step_id = 9999

if else statement not returning any value if mySQL row is empty?

Just check if $comments is empty. You don't need to check every time through the loop, since the loop won't run if there are no comments.

if (empty($comments)) {
echo "No comments yet";
} else {
foreach ($comments as $comment) {
?>
<li class="list-group-item">
<div class="h6 m-0">@<?php echo "$comment->username" ?></div>
<div class="text-muted"> <i class="fa fa-clock-o mr-1"></i><?php echo "$comment->comment_time" ?></div>
<p class="card-text"><?php echo "$comment->comment" ?></p>
</li>
<?php
}
}


Related Topics



Leave a reply



Submit