[英]MySQL - Search PHP script
我正在編寫自己的CMS編碼,而我正處於search.php
。 我需要在一個查詢中搜索4個具有不同列名的表,因為我的服務器不是最快的。
有我的數據庫安排:
cmw_admin
(用戶表)
+-----------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| role | int(11) | NO | | 1 | |
| username | text | NO | | NULL | |
| password | text | NO | | NULL | |
| email | text | NO | | NULL | |
| phone | text | NO | | NULL | |
| location | text | NO | | NULL | |
| full_name | text | NO | | NULL | |
| bio | text | NO | | NULL | |
| website | text | NO | | NULL | |
| last_login | datetime | NO | | NULL | |
| registered_date | datetime | NO | | NULL | |
| registered_ip | text | NO | | NULL | |
| credits | tinyint(1) | NO | | NULL | |
| online | int(11) | NO | | 0 | |
+-----------------+------------+------+-----+---------+----------------+
cmw_blog
(文章表)
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | text | NO | | NULL | |
| content | text | NO | | NULL | |
| date | datetime | NO | | NULL | |
| author | int(11) | NO | | NULL | |
| categories | text | NO | | NULL | |
| media | text | NO | | NULL | |
| thumb | text | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
cmw_projects
(投資組合)
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| owner_id | int(11) | NO | | NULL | |
| title | text | NO | | NULL | |
| content | text | NO | | NULL | |
| date | date | NO | | NULL | |
| link | text | NO | | NULL | |
| img | text | NO | | NULL | |
| type | int(11) | NO | | NULL | |
| start_date | date | NO | | NULL | |
| end_date | date | NO | | NULL | |
| done | tinyint(1) | NO | | 0 | |
| screenshots | text | NO | | NULL | |
+-------------+------------+------+-----+---------+----------------+
cmw_services
(服務表)
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cat_id | int(11) | NO | | NULL | |
| name | text | NO | | NULL | |
| description | text | NO | | NULL | |
| img | text | NO | | NULL | |
| price | decimal(11,2) | NO | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
在我的搜索中,我需要在每個name
/ title
列以及所有description
/ content
列中搜索字符串。 很明顯,我想在一個查詢中搜索所有這些表中的一個字符串。 那可能嗎? 我知道使用JOIN
, LEFT JOIN
, RIGHT JOIN
和INNER JOIN
可能很容易,但我不知道如何使用它們。 谷歌對此並不十分明確!
四個直接查詢的總成本很可能遠低於JOIN的成本加上對那些相同記錄/字段的搜索。
如果有的話,檢查FULLTEXT索引的可能性。
在任何情況下,你都可以這樣做
SELECT "cmw_admin" as source, id FROM cmw_admin, NULL as date [...] WHERE bio LIKE '%search%'
UNION
SELECT "cmw_blog" as source, id FROM cmw_admin, date as date [...] WHERE
(title LIKE '%search%' OR content LIKE '%search%')
UNION
...
它將為您提供已找到記錄的列表,以及記錄來自何處的提示(“cmw_admin”,“cmw_blog”,...); 這將允許您選擇如何顯示各種來源的記錄。
查詢將始終返回相同的字段集,一些字段具有有效值,一些字段不具有,具體取決於“源”的內容。 然后在PHP腳本中,您可以執行類似的操作,
$source = $record['source'];
if (!isset($template[$source]))
$template[$source] = file_get_contents("templates/search/$source.html");
$html_view = preg_replace('#{{\s*([^}\s]*)\s*}}#e', 'isset(\$record["\1"])?\$vars["\1"]:""', $template[$source]);
這將采用包含HTML片段的HTML模板
<h2>{{ date }}</h2>
並使用當前$記錄的[date]條目“填充”該字段。 我發現這是保持HTML和代碼分離的好方法(YMMV)。 如果你已經有一個模板系統,你可以(而且應該!)改編那個。
如果你只想在mysql中搜索,那么FULLTEXT索引就是你的選擇。
在搜索時,您應該使用單個查詢pr表,因為您很可能需要根據找到匹配項的表來構建不同的鏈接/結果html。
但你應該考慮尋找像Apache Lucene這樣的真實搜索引擎。 Zend有一個非常好的實現,它不依賴於任何外部服務http://framework.zend.com/manual/en/zend.search.lucene.html/它將有助於對結果進行更好的排名比你可以做的更好mysql,它應該更好地擴展。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.