簡體   English   中英

查詢聯合,如果搜索結果小於10,則按日期從數據庫順序中填充其他

[英]Query union, if search result is less than 10, fill with others from database order by date

我想進行mysql聯合搜索。 我的目的是:我的總成績必須是10。

  • 如果搜索結果大於10,則返回的數據全部來自搜索結果。
  • 如果搜索結果小於10,則返回的前幾個數據來自搜索結果,然后按日期從數據庫順序中獲取其余結果。

更清楚地說:如果客戶搜索“今天”,則我的數據庫僅返回7個包含“今天”的結果,然后從數據庫ORDER BY日期中添加另外3個結果。 這樣一來,總結果為10個項目。

另一個目的:另外3個結果不是與搜索匹配的7個結果的重復。 我認為UNIONUNION DISTINCT可以完成這項工作,對嗎?

那么,我該如何進行查詢?

PS:我的代碼將確定結果順序,但是我需要第一個select始終位於第二個select

(SELECT * FROM table WHERE title like %$searchword% limit 0,10 ORDER BY date)
UNION
(SELECT * FROM table limit 0,10 ORDER BY date)
limit 0,10 ORDER BY date

如果您始終想要10個結果:

SELECT 
    IF(m.id,1,0) AS has_match,
    t.*
FROM 
    `table` t
    LEFT JOIN `table` m ON m.id = t.id AND m.title LIKE '%$searchword%'
GROUP BY t.id
ORDER BY has_match DESC, date
LIMIT 10


經過測試:

mysql> select * from `table`;
+----+------------------------+---------------------+
| id | title                  | date                |
+----+------------------------+---------------------+
|  1 | test 1                 | 2011-11-06 10:27:08 |
|  2 | test 2 match           | 2011-11-06 10:27:14 |
|  3 | 3 match this too       | 2011-11-06 10:27:23 |
|  4 | title does NOT         | 2011-11-06 10:27:44 |
|  5 | Another matching title | 2011-11-06 10:27:55 |
|  6 | this does not either   | 2011-11-06 10:29:22 |
|  7 | Do not put this first  | 2011-11-06 10:29:37 |
|  8 | Is this number 8?      | 2011-11-06 10:29:57 |
|  9 | The 9th is a match     | 2011-11-06 10:30:07 |
| 10 | 10th does not          | 2011-11-06 10:30:20 |
| 11 | 11th IS a match too!   | 2011-11-06 10:30:37 |
| 12 | 12th gets ignored?     | 2011-11-06 10:30:49 |
+----+------------------------+---------------------+
12 rows in set (0.00 sec)

mysql> SELECT IF(m.id,1,0) AS has_match, t.* FROM `table` t LEFT JOIN `table` m ON m.id = t.id AND m.title LIKE '%match%' GROUP BY t.id ORDER BY has_match DESC, date LIMIT 10;
+-----------+----+------------------------+---------------------+
| has_match | id | title                  | date                |
+-----------+----+------------------------+---------------------+
|         1 |  2 | test 2 match           | 2011-11-06 10:27:14 |
|         1 |  3 | 3 match this too       | 2011-11-06 10:27:23 |
|         1 |  5 | Another matching title | 2011-11-06 10:27:55 |
|         1 |  9 | The 9th is a match     | 2011-11-06 10:30:07 |
|         1 | 11 | 11th IS a match too!   | 2011-11-06 10:30:37 |
|         0 |  1 | test 1                 | 2011-11-06 10:27:08 |
|         0 |  4 | title does NOT         | 2011-11-06 10:27:44 |
|         0 |  6 | this does not either   | 2011-11-06 10:29:22 |
|         0 |  7 | Do not put this first  | 2011-11-06 10:29:37 |
|         0 |  8 | Is this number 8?      | 2011-11-06 10:29:57 |
+-----------+----+------------------------+---------------------+
10 rows in set (0.00 sec)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM