我如何验证两个查询,如果第一个查询是NULL继续第二个查询?
第一查询:
SELECT max(logs) as logs,id FROM table1 WHERE message NOT LIKE "[ SYSTEM%"
第二个查询:
SELECT max(logs) as logs,id FROM table2 WHERE message LIKE "[ SYSTEM%"
我想你应该看看你的解释.因为对db的查询计数较少 – 更好.在这种情况下,您应该使用union:
原文链接:https://www.f2er.com/php/134167.htmlSELECT max(logs) as logs,id,'table1' type FROM table1 WHERE message NOT LIKE "[ SYSTEM%" UNION SELECT max(logs) as logs,'table2' type FROM table2 WHERE message LIKE "[ SYSTEM%" ;
按字段类型,您可以了解从哪个表接收数据.
但如果你的解释不好,你应该使用单独的查询:
<?PHP $dbh = new PDO('MysqL:dbname=test;host=127.0.0.1','root'); $sth = $dbh->prepare(' SELECT max(logs) as logs,id FROM table1 WHERE message NOT LIKE :like '); $sth->bindParam(':like',$like,PDO::PARAM_STR); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); if (empty($result)) { $sth = $dbh->prepare(' SELECT max(logs) as logs,id FROM table2 WHERE message LIKE :like '); $sth->bindParam(':like',PDO::PARAM_STR); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_ASSOC); } var_export($result);