[英]Combining if statements and multiple sql queries into one query
我有一台服務器,它限制了你每小時可以進行的 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.