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
Query: Fetch 3 Records Which Has Higher Value
How to Replace Empty String With Value That Is Not Empty for the Same Policynumber
Sqlcmd Not Able to Find a Library (Libmsodbcsql-17.0.So.1.1) That Is There
Presto SQL - Converting a Date String to Date Format
How to Return a Flag If Exist Id in Another Table MySQL
How to Count Number of Digits After a Decimal Place
Mysql: Alter Table If Column Not Exists
How to Limit Results of a Left Join
Select Only Rows That Contain Only Alphanumeric Characters in MySQL
Select and Compare Two Datetime Columns from Different Table Without Having Any Relation
Retrieve String Between HTML Tags Using Regex
Mysql - Trigger for Updating Same Table After Insert
How to Find Specific Values in a Table in Oracle
Removing Null Value in SQL Join and Union Operators in SQL
Daily Report by Date With Mssql for Mutiple Column
A SQL Query to Get All the Records Where 5 Columns Are Same But Only One Column Is Different