Ver Mensaje Individual
  #6 (permalink)  
Antiguo 07/08/2013, 10:37
aprendiz69
 
Fecha de Ingreso: junio-2008
Mensajes: 8
Antigüedad: 15 años, 11 meses
Puntos: 0
Respuesta: Actualizar datos de un campo con datos de varios campos

Gracias, Leo,

Intentaré explicar lo mejor posible las características de las tablas que me interesan (lo de la cardinalidad 1-1 vs 1-n ni tan siquiera sabía lo que era: recuerda mi ignorancia cuasiabsoluta en materia de MySQL)).

En mi primer hilo sólo hice referencia a dos tablas, en este segundo hilo ya incluí una tercera y ahora incluyo la cuarta y última que me interesa. No es que las desconociera al principio, es que como no tengo ni idea de MySQL preferí ir poquito a poco, empezar y comprender cosas sencillas, antes de liarme con las más complejas (mi planteamiento no era llegar aquí y pedir que me resolvieran lo complejo, sino solicitar ayuda para ir comprendiendo y encontrando soluciones, paso a paso, desde lo más sencillo hasta lo más complicado). Ahora las pongo todas, ya que me has solicitado información detallada.

Primera tabla (wp_posts)

Código:
CREATE TABLE IF NOT EXISTS `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_author` bigint(20) NOT NULL DEFAULT '0',
  `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content` longtext NOT NULL,
  `post_title` text NOT NULL,
  `post_category` int(4) NOT NULL DEFAULT '0',
  `post_excerpt` text NOT NULL,
  `post_status` varchar(20) NOT NULL DEFAULT 'publish',
  `comment_status` varchar(20) NOT NULL DEFAULT 'open',
  `ping_status` varchar(20) NOT NULL DEFAULT 'open',
  `post_password` varchar(20) NOT NULL DEFAULT '',
  `post_name` varchar(200) NOT NULL DEFAULT '',
  `to_ping` text NOT NULL,
  `pinged` text NOT NULL,
  `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `post_content_filtered` text NOT NULL,
  `post_parent` bigint(20) NOT NULL DEFAULT '0',
  `guid` varchar(255) NOT NULL DEFAULT '',
  `menu_order` int(11) NOT NULL DEFAULT '0',
  `post_type` varchar(20) NOT NULL DEFAULT 'post',
  `post_mime_type` varchar(100) NOT NULL DEFAULT '',
  `comment_count` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `post_name` (`post_name`),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
  KEY `post_parent` (`post_parent`)
)
Esta tabla es la que incluye la información principal de cada post (ID, autor, título, contenido, etc.).

De esta tabla, el campo que me interesa es el primero: ID (el identificador de cada post).

Del campo post_category podemos olvidarnos, ya que sólo incluye un número entero correspondiente a una categoría, mientras que hay muchos posts que tienen varias categorías (este campo creo que es una herencia de antiguas versiones WP, antes de que éste empezara a usar taxonomy).

Ejemplo de selección de lo que me interesa:

Código:
mysql> SELECT ID FROM `wp_posts` LIMIT 0 , 10;
+----+
| ID |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
+----+
10 rows in set (0.00 sec)
Segunda tabla (wp_terms)

Código:
CREATE TABLE IF NOT EXISTS `wp_terms` (
  `term_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL DEFAULT '',
  `slug` varchar(200) NOT NULL DEFAULT '',
  `term_group` bigint(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`term_id`),
  UNIQUE KEY `slug` (`slug`),
  KEY `name` (`name`)
)
Esta tabla es la que incluye la información general de los términos (categorías y etiquetas), pero sin diferenciar entre sus distintos tipos (category, post_tag).

Los campos que me interesan son term_id y name.

El valor del campo term_id de esta tabla siempre es igual al de su campo homónimo de la tabla wp_term_taxonomy (wp_terms.term_id = wp_term_taxonomy.term_id).

Ejemplo de selección de lo que me interesa:

Código:
mysql> SELECT term_id, name FROM `wp_terms` LIMIT 0 , 10;
+---------+-------------+
| term_id | name        |
+---------+-------------+
|       1 | Categoría 1 |
|       2 | Categoría 2 |
|       3 | Categoría 3 |
|       4 | Categoría 4 |
|       5 | Categoría 5 |
|       6 | Categoría 6 |
|       7 | Etiqueta 1  |
|       8 | Etiqueta 2  |
|       9 | Etiqueta 3  |
|      10 | Etiqueta 4  |
+---------+-------------+
10 rows in set (0.00 sec)
Tercera tabla (wp_term_relationships)

Código:
CREATE TABLE IF NOT EXISTS `wp_term_relationships` (
  `object_id` bigint(20) NOT NULL DEFAULT '0',
  `term_taxonomy_id` bigint(20) NOT NULL DEFAULT '0',
  `term_order` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`object_id`,`term_taxonomy_id`),
  KEY `term_taxonomy_id` (`term_taxonomy_id`)
)
Esta tabla es la que relaciona los posts (object_id) con los taxones (term_taxonomy_id).

El valor del campo object_id de esta tabla siempre es igual al del campo ID de la tabla wp_posts (wp_term_relationships.object_id = wp_posts.ID).

El valor del campo term_taxonomy_id de esta tabla siempre es igual al de su campo homónimo de la tabla wp_term_taxonomy (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id).

Como vemos abajo, un mismo post (object.id) puede estar relacionado con varios taxones (term_taxonomy_id).

Ejemplo de selección de lo que me interesa:

Código:
mysql> SELECT object_id, term_taxonomy_id FROM `wp_term_relationships` LIMIT 0 , 10;
+-----------+------------------+
| object_id | term_taxonomy_id |
+-----------+------------------+
|         1 |                1 |
|         2 |                1 |
|         2 |                2 |
|         3 |                1 |
|         3 |                2 |
|         3 |                3 |
|         4 |                1 |
|         4 |                2 |
|         4 |                3 |
|         4 |                4 |
+-----------+------------------+
10 rows in set (0.00 sec)
Cuarta tabla (wp_term_taxonomy)

Código:
CREATE TABLE IF NOT EXISTS `wp_term_taxonomy` (
  `term_taxonomy_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `term_id` bigint(20) NOT NULL DEFAULT '0',
  `taxonomy` varchar(32) NOT NULL DEFAULT '',
  `description` longtext NOT NULL,
  `parent` bigint(20) NOT NULL DEFAULT '0',
  `count` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`term_taxonomy_id`),
  UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`)
)
Esta tabla es la que relaciona taxones, términos y taxonomía (tipo de taxón).

De ella sólo me interesan tres campos: term_taxonomy_id, term_id y taxonomy.

El valor del campo term_taxonomy_id de esta tabla siempre es igual al del campo homónimo de la tabla wp_term_relationships (wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id).

El valor del campo term_id de esta tabla siempre es igual al de su campo homónimo de la tabla wp_terms (wp_term_taxonomy.term_id = wp_terms.term_id).

En cambio, los valores de los campos term_taxonomy_id y term_id de esta tabla pueden ser distintos.

Ejemplo de selección de lo que me interesa:

Código:
mysql> SELECT term_taxonomy_id, term_id, taxonomy FROM `wp_term_taxonomy` LIMIT 0 , 10;
+------------------+---------+----------+
| term_taxonomy_id | term_id | taxonomy |
+------------------+---------+----------+
|                1 |       1 | category |
|                2 |       2 | category |
|                3 |       3 | category |
|                4 |       4 | category |
|                5 |       5 | category |
|                6 |       6 | category |
|                7 |       7 | post_tag |
|                8 |       8 | post_tag |
|                9 |       9 | post_tag |
|               10 |      10 | post_tag |
+------------------+---------+----------+
10 rows in set (0.00 sec)
Finalmente, en respuesta a tus preguntas:
1.- Todos los elementos de la tabla wp_posts tienen relación con uno o más elementos de la tabla wp_term_relationships.
2.- Cada elemento de la tabla wp_term_relationships tiene relación con un elemento de la tabla wp_term_taxonomy.
3.- Cada elemento de la tabla wp_term_taxonomy tiene relación con un elemento de la tabla wp_terms.

Sigo en el próximo mensaje, que éste tenía más de 10.000 caracteres...