我目前正在编写自己的CMS,并且我在search.php
处。我只需在一个查询中搜索4个具有不同列名的表,因为我的服务器不是最快的。MySQL - 搜索PHP脚本
有我的DB安排:
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
可能很容易,但我不知道如何使用它们。谷歌对这些事情不是很明确!
+1格式不错。 – Lion 2012-07-16 17:51:28
使用连接很容易,但任何匹配都会返回所有4个表中的每一列。我不认为这是有意的行为。无论你的服务器有多慢,你都应该做4个查询或者重新构建你的数据库 – 2012-07-16 17:51:45