我目前正在编写我自己的 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>
并使用当前 $record 的 [date] 条目“填充”该字段。我发现这是保持 HTML 和代码分离 (YMMV) 的好方法。如果您已经有一个模板系统,您可以(并且应该!)改用它。
关于php - MySQL - 搜索 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509707/