php – 如果子查询的结果为NULL,则整个查询失败

前端之家收集整理的这篇文章主要介绍了php – 如果子查询的结果为NULL,则整个查询失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一种情况,我需要从同一个表的有效数据中生成包含随机数据的行

我通过PHP rand($min,$max)函数生成ID,$min = 1,$max = [SELECT MAX(ID)FROM patient] – 1

Select

tblFirstName.FirstName,tblLastName.LastName,tblBirthDate.BirthDate,tblLocation.Location

From

(SELECT FirstName FROM patient WHERE ID > 11445 AND FirstName != '' LIMIT 1) AS tblFirstName,(SELECT LastName FROM patient WHERE ID > 74964 AND LastName != '' LIMIT 1) AS tblLastName,(SELECT BirthDate FROM patient WHERE ID > 26360 LIMIT 1) AS tblBirthDate,(SELECT Location FROM patient WHERE ID > 68356 AND Location != '' LIMIT 1) AS tblLocation

现在在ID>从上述查询26360得到的26360是随机数>用于避免26360被删除的可能性

问题:

如果任何子查询返回没有结果,则完成查询失败并且不返回任何内容.

试试这个:
SELECT (SELECT FirstName FROM patient WHERE ID > 11445 AND FirstName != '' LIMIT 1) AS tblFirstName,(SELECT Location FROM patient WHERE ID > 68356 AND Location != '' LIMIT 1) AS tblLocation

猜你在找的PHP相关文章