Simple check for SELECT query empty result
Use @@ROWCOUNT:
SELECT * FROM service s WHERE s.service_id = ?;
IF @@ROWCOUNT > 0
-- do stuff here.....
According to SQL Server Books Online:
Returns the number of rows affected by
the last statement. If the number of
rows is more than 2 billion, use
ROWCOUNT_BIG.
SQL return result on empty query
IF NOT EXISTS(SELECT Name FROM Contact WHERE Age > @age)
BEGIN
SELECT '' AS age
END
ELSE
BEGIN
SELECT Name FROM Contact WHERE Age > @age
END
This will check if a result set will return and if nothing will, select a blank space and name the field age.
SELECT query return error on Empty result
Try replacing
FIND_IN_SET(`Notification`.`id`,(SELECT read_ids FROM read_notifications WHERE user_id = 46))
with
FIND_IN_SET(`Notification`.`id`,IFNULL((SELECT read_ids FROM read_notifications WHERE user_id = 46), ''))
From the manual, if the subquery result is empty it will return NULL, and FIND_IN_SET will return NULL if either argument is NULL, and NOT NULL is still NULL, as is NULL AND 1, and NULL is equivalent to false. So when the subquery returns NULL, your WHERE condition will fail.
By adding an IFNULL around the subquery result, you can get it to return a value which is valid for FIND_IN_SET, which will allow your query to work as expected.
Check empty result with mysql query
Try on below:
select * from section s where idsection not in (select page.idsection from
page where
page.idsection=s.idsection and idpage not in (select idpage from read));
I use not in because u need to check all pages instead of 1 of the page.
So we should get the page that are not in read (unread) and put not in there.
If u have 1 section that have 5 pages and all is read, it will give you empty result on subquery making ur main query selecting the session.
If u have 1 section that have 10 pages that only 9 pages is read, it will give u pages no 10 in subquery and make your section in main query empty (not in).
How do I check if a table is empty or already my query didn't match any results
I guess, that $conn
is a PDO connection? In that case, the method $conn->query()
returns an object of type PDOStatement. See https://www.php.net/manual/de/class.pdostatement.php
The method does NOT return the result set.
Instead you can use the PDOStatement object, to fetch the results:
$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetchAll();
if(empty($result))
{ ... }
In case you are using mysqli, the object returned by query()
is this: https://www.php.net/manual/en/class.mysqli-result.php
So the code would be:
$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);
if(empty($result))
{ ... }
Please also note: Your code is highly insecure! You should use prepared statements to prevent sql-injection:
$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = :currentUrl";
$stmt = $conn->prepare($sql);
$stmt->execute(['currentUrl' => $currentURL]);
$result = $stmt->fetchAll();
if(empty($result))
{ ... }
select query in mysql always return empty result
It's because the values are NULL
. NULL
cannot be captured by an =
or !=
comparison. Use the following instead:
SELECT *
FROM `rdm_order`
WHERE `aff_result` != 'xyc'
OR `aff_result` IS NULL
Related Topics
Replace Default Null Values Returned from Left Outer Join
Postgresql - Combining Multiple Rows With Several Attributes into One Row
Spring Data JPA Utf-8 Encoding Not Working
Return Dummy Value If No Row Present for in Clause
Sql Method to Replace Repeating Blanks With Single Blanks
How to Find Multiple Occurrence of Particular String and Fetch Value in SQL Server
Spark - Query Dataframe Based on Values from a Column in Another Dataframe
Fetch the Rows Which Have the Max Value for a Column for Each Distinct Value of Another Column
Removing Leading Zeroes from a Field in a SQL Statement
I Want to Give Serial No in My Query According to Group
Convert a 12 Hour Format to 24 Hour Format in SQL Server
Calculating Age Derived from Current Date and Dob
How to Connect MySQL Workbench to Running MySQL Inside Docker
Count Table Difference Between Two Tables by a Complex Key in Hive
How to Get the Month and Day With Leading 0'S in SQL (E.G. 9 => 09)