how can i validate two queries, if the first query is NULL proceed to second query?
我如何验证两个查询,如果第一个查询是NULL继续第二个查询?
1st query:
SELECT max(logs) as logs,id FROM table1 WHERE
message NOT LIKE "[ SYSTEM%"
2nd query:
SELECT max(logs) as logs,id FROM table2 WHERE
message LIKE "[ SYSTEM%"
3 个解决方案
#1
1
I think you should look to your explain. Because less count queries to db - better. In this case you should use union:
我想你应该看看你的解释。因为对db的查询计数较少 - 更好。在这种情况下,您应该使用union:
SELECT max(logs) as logs, id, 'table1' type
FROM table1 WHERE message NOT LIKE "[ SYSTEM%"
UNION
SELECT max(logs) as logs, id, 'table2' type
FROM table2 WHERE message LIKE "[ SYSTEM%"
;
by field type you can understand from which table you receive data.
But if your explain will be bad, you should use separate queries:
通过字段类型,您可以了解从哪个表接收数据。但如果你的解释不好,你应该使用单独的查询:
<?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', $like, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
}
var_export($result);
#2
1
This query looks ugly, but I think you can do it
这个查询看起来很难看,但我认为你可以做到
(SELECT max(logs) as logs,id
FROM table1
WHERE message NOT LIKE "[ SYSTEM%")
UNION ALL
(SELECT max(logs) as logs,id
FROM table2
WHERE message LIKE "[ SYSTEM%")
You can imagine how faster we can get the records if used only one query instead of two.
您可以想象,如果只使用一个查询而不是两个查询,我们可以更快地获取记录。
you can also add flag in result (just to check if record from table 1 or table 2 )
你也可以在结果中添加标志(只是为了检查表1或表2中的记录)
#3
0
$query1 = "SELECT max(logs) as logs,id FROM table1 WHERE
message NOT LIKE '[ SYSTEM%'";
$query2 = SELECT max(logs) as logs,id FROM table2 WHERE
message LIKE '[ SYSTEM%'";
if(mysql_query($query1)) {
$result = mysql_query($query1);
} else {
$result = mysql_query($query2);
}
#1
1
I think you should look to your explain. Because less count queries to db - better. In this case you should use union:
我想你应该看看你的解释。因为对db的查询计数较少 - 更好。在这种情况下,您应该使用union:
SELECT max(logs) as logs, id, 'table1' type
FROM table1 WHERE message NOT LIKE "[ SYSTEM%"
UNION
SELECT max(logs) as logs, id, 'table2' type
FROM table2 WHERE message LIKE "[ SYSTEM%"
;
by field type you can understand from which table you receive data.
But if your explain will be bad, you should use separate queries:
通过字段类型,您可以了解从哪个表接收数据。但如果你的解释不好,你应该使用单独的查询:
<?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', $like, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
}
var_export($result);
#2
1
This query looks ugly, but I think you can do it
这个查询看起来很难看,但我认为你可以做到
(SELECT max(logs) as logs,id
FROM table1
WHERE message NOT LIKE "[ SYSTEM%")
UNION ALL
(SELECT max(logs) as logs,id
FROM table2
WHERE message LIKE "[ SYSTEM%")
You can imagine how faster we can get the records if used only one query instead of two.
您可以想象,如果只使用一个查询而不是两个查询,我们可以更快地获取记录。
you can also add flag in result (just to check if record from table 1 or table 2 )
你也可以在结果中添加标志(只是为了检查表1或表2中的记录)
#3
0
$query1 = "SELECT max(logs) as logs,id FROM table1 WHERE
message NOT LIKE '[ SYSTEM%'";
$query2 = SELECT max(logs) as logs,id FROM table2 WHERE
message LIKE '[ SYSTEM%'";
if(mysql_query($query1)) {
$result = mysql_query($query1);
} else {
$result = mysql_query($query2);
}