Ver Mensaje Individual
  #2 (permalink)  
Antiguo 30/06/2009, 13:36
Avatar de huesos52
huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: LLevar datos de una tabla a otra con condición

Un ejemplo vale mas que mil palabras.

Código mysql:
Ver original
  1. mysql> select *from paciente;
  2. +-------------+-------------+------+-------+
  3. | id_paciente | nombre      | hist | fecha |
  4. +-------------+-------------+------+-------+
  5. |           1 | daniel      | NULL | NULL  |
  6. |           2 | santiago    | NULL | NULL  |
  7. |           3 | juan manuel | NULL | NULL  |
  8. |           4 | dondi       | NULL | NULL  |
  9. +-------------+-------------+------+-------+
  10. 4 rows in set (0.01 sec)
  11.  
  12. mysql> select *from hist_clinica;
  13. +------+-------------+------------+
  14. | hist | id_paciente | fecha      |
  15. +------+-------------+------------+
  16. |    1 |           1 | 2009-06-30 |
  17. |    2 |           3 | 2009-06-30 |
  18. |    3 |           4 | 2009-06-30 |
  19. |    4 |           2 | 2009-06-30 |
  20. +------+-------------+------------+
  21. 4 rows in set (0.00 sec)
  22.  
  23. mysql> update paciente p inner join hist_clinica hc
  24. ->using(id_paciente) set p.hist=hc.hist, p.fecha=hc.fecha;
  25. Query OK, 4 rows affected (0.00 sec)
  26. Rows matched: 4  Changed: 4  Warnings: 0
  27.  
  28. mysql> select *from paciente;
  29. +-------------+-------------+------+------------+
  30. | id_paciente | nombre      | hist | fecha      |
  31. +-------------+-------------+------+------------+
  32. |           1 | daniel      |    1 | 2009-06-30 |
  33. |           2 | santiago    |    4 | 2009-06-30 |
  34. |           3 | juan manuel |    2 | 2009-06-30 |
  35. |           4 | dondi       |    3 | 2009-06-30 |
  36. +-------------+-------------+------+------------+
  37. 4 rows in set (0.00 sec)
  38.  
  39. mysql> drop table hist_clinica;
  40. Query OK, 0 rows affected (0.00 sec)

Cuentanos como te va.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming

Última edición por huesos52; 30/06/2009 a las 14:07