gpt4 book ai didi

php - 将 if 语句和多个 sql 查询合并为一个查询

转载 作者:行者123 更新时间:2023-11-29 02:20:34 26 4
gpt4 key购买 nike

我有一个服务器,它限制了每小时可以进行的 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;
}

最佳答案

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。

关于php - 将 if 语句和多个 sql 查询合并为一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com