我有一个服务器,它限制了你每小时可以进行的sql查询的数量,所以我试图找到一种方法将我的所有3个if语句和sql查询组合成一个sql查询,并且我想知道这是否可能?请看一下我的代码:
$text1 = "one";
$text2 = "two";
$text3 = "three";
$data = SELECT text FROM tableName WHERE
text LIKE '%$text1%' AND
text LIKE '%$text2%' AND
text LIKE '%$text3%' order by rand() limit 1;
if ($data == null) {
$data = SELECT text FROM tableName WHERE
text LIKE '%$text1%' AND
text LIKE '%$text2%' order by rand() limit 1;
}
if ($data == null) {
$data = SELECT text FROM tableName WHERE
text LIKE '%$text1%' order by rand() limit 1;
}
答案 0 :(得分:2)
SELECT text,
((text LIKE '%$text1%') + (text LIKE '%$text2%') + (text LIKE '%$text3%')) as `matches`
FROM tableName
HAVING `matches` > 0
ORDER BY `matches` DESC, rand() LIMIT 1
这应该按匹配数对值进行排序,因为每个表达式在匹配时将被视为整数1,否则为0。