Actualmente estoy codificando mi propio CMS y estoy en el punto del search.php
. Necesito buscar en 4 tablas que tienen un nombre de columna diferente en una sola consulta porque mi servidor no es el más rápido.MySQL - Buscar script PHP
Ahí está mi disposición DB:
cmw_admin
(tabla de usuarios)
+-----------------+------------+------+-----+---------+----------------+
| 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
(tabla articles)
+------------+----------+------+-----+---------+----------------+
| 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
(cartera)
+-------------+------------+------+-----+---------+----------------+
| 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
(mesa de servicios)
+-------------+---------------+------+-----+---------+----------------+
| 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 | |
+-------------+---------------+------+-----+---------+----------------+
En mi búsqueda, tengo que buscar cada cadena en name
/title
columnas y en todos los description
/content
columnas también. En claro, quiero buscar todas esas tablas para una cadena, en una sola consulta. ¿Es eso posible? Sé que usar JOIN
, LEFT JOIN
, RIGHT JOIN
y INNER JOIN
puede ser fácil pero no sé cómo usarlos. ¡Google no es muy explícito en esas cosas!
+1 para un buen formato. – Lion
Es bastante fácil de hacer con una unión, pero cualquier coincidencia devolvería cada columna de las 4 tablas. No creo que ese sea el comportamiento previsto. No importa qué tan lento sea su servidor, debería hacer 4 consultas O reestructurar su base de datos –