Foros del Web » Programando para Internet » PHP »

Consulta en varias tablas.

Estas en el tema de Consulta en varias tablas. en el foro de PHP en Foros del Web. Buenos dias, por favor quiero hacer una consulta a varias tablas en las que guardo las facturas (facturas_2011,facturas_2012) . Ahora puedo hacer solo a una. ...
  #1 (permalink)  
Antiguo 10/01/2013, 02:44
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Consulta en varias tablas.

Buenos dias, por favor quiero hacer una consulta a varias tablas en las que guardo las facturas (facturas_2011,facturas_2012) . Ahora puedo hacer solo a una.
Código MySQL:
Ver original
  1. $result = mysql_query("SELECT SUM(material) as material FROM  facturas WHERE realizacion BETWEEN '$FInicio' AND '$FFin'  ");

He intentado así, pero no funciona:

Código MySQL:
Ver original
  1. $result = mysql_query("SELECT SUM(material) as material FROM  facturas,facturas_2011,facturas_2012 WHERE realizacion BETWEEN '$FInicio' AND '$FFin'  ");

Gracias y un saludo.
  #2 (permalink)  
Antiguo 10/01/2013, 02:52
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Código MySQL:
Ver original
  1. SELECT SUM(material) as material
  2. FROM (SELECT IFNULL(SUM(material),0) as material FROM  facturas WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
  3. SELECT IFNULL(SUM(material),0) as material FROM  facturas_2011 WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
  4. SELECT IFNULL(SUM(material),0) as material FROM  facturas_2012 WHERE realizacion BETWEEN '$FInicio' AND '$FFin')

o

Código MySQL:
Ver original
  1. SELECT SUM(material) as material
  2. FROM (SELECT realizacion,material FROM  facturas
  3.            UNION ALL
  4.            SELECT realizacion,material FROM  facturas_2011
  5.            UNION ALL
  6.           SELECT realizacion,material FROM  facturas_2012)
  7. WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.
  #3 (permalink)  
Antiguo 10/01/2013, 03:31
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Cita:
Iniciado por quimfv Ver Mensaje
Código MySQL:
Ver original
  1. SELECT SUM(material) as material
  2. FROM (SELECT IFNULL(SUM(material),0) as material FROM  facturas WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
  3. SELECT IFNULL(SUM(material),0) as material FROM  facturas_2011 WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
  4. SELECT IFNULL(SUM(material),0) as material FROM  facturas_2012 WHERE realizacion BETWEEN '$FInicio' AND '$FFin')

o

Código MySQL:
Ver original
  1. SELECT SUM(material) as material
  2. FROM (SELECT realizacion,material FROM  facturas
  3.            UNION ALL
  4.            SELECT realizacion,material FROM  facturas_2011
  5.            UNION ALL
  6.           SELECT realizacion,material FROM  facturas_2012)
  7. WHERE realizacion BETWEEN '$FInicio' AND '$FFin'
Gracias, lo he puesto así:
Código MySQL:
Ver original
  1. $result = mysql_query(" SELECT SUM(material) as material
  2. FROM (SELECT realizacion,material FROM  facturas
  3.           UNION ALL
  4.           SELECT realizacion,material FROM  facturas_2011
  5.           UNION ALL
  6.          SELECT realizacion,material FROM  facturas_2012)
  7. WHERE realizacion BETWEEN '$FInicio' AND '$FFin' ");
  8.  
  9.  $row = mysql_fetch_array($result, MYSQL_ASSOC);

Pero me da error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource en esta linea:
Código HTML:
Ver original
  1. $row = mysql_fetch_array($result, MYSQL_ASSOC);

Gracias y un saludo.

Última edición por satjaen; 10/01/2013 a las 03:38
  #4 (permalink)  
Antiguo 10/01/2013, 05:28
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Código PHP:
Ver original
  1. //tu versión
  2. echo " SELECT SUM(material) as material
  3. FROM (SELECT realizacion,material FROM  facturas
  4.           UNION ALL
  5.           SELECT realizacion,material FROM  facturas_2011
  6.           UNION ALL
  7.          SELECT realizacion,material FROM  facturas_2012)
  8. WHERE realizacion BETWEEN '$FInicio' AND '$FFin' <br />";
  9.  
  10.  
  11. $result = mysql_query("SELECT SUM(datos.material) as material ".
  12.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  13.     "UNION ALL ".
  14.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  15.     "UNION ALL ".
  16.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  17.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';");
  18.  
  19.  
  20. //versión mejorada
  21. echo "SELECT SUM(datos.material) as material ".
  22.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  23.     "UNION ALL ".
  24.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  25.     "UNION ALL ".
  26.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  27.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';<br />"



Pon los alias... ejecuta la query directamente en el servidor para ver que error te da.... imprime la sentencia y asegurate que hay los blancos que tiene que haber...

"SELECT SUM(datos.material) as material ".<--este blanco (despues de material y antes de las ")

y cambia de equipo quizas es culpa de MOU
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.

Última edición por quimfv; 10/01/2013 a las 05:43
  #5 (permalink)  
Antiguo 10/01/2013, 06:49
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Cita:
Iniciado por quimfv Ver Mensaje
Código PHP:
Ver original
  1. //tu versión
  2. echo " SELECT SUM(material) as material
  3. FROM (SELECT realizacion,material FROM  facturas
  4.           UNION ALL
  5.           SELECT realizacion,material FROM  facturas_2011
  6.           UNION ALL
  7.          SELECT realizacion,material FROM  facturas_2012)
  8. WHERE realizacion BETWEEN '$FInicio' AND '$FFin' <br />";
  9.  
  10.  
  11. $result = mysql_query("SELECT SUM(datos.material) as material ".
  12.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  13.     "UNION ALL ".
  14.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  15.     "UNION ALL ".
  16.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  17.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';");
  18.  
  19.  
  20. //versión mejorada
  21. echo "SELECT SUM(datos.material) as material ".
  22.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  23.     "UNION ALL ".
  24.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  25.     "UNION ALL ".
  26.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  27.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';<br />"



Pon los alias... ejecuta la query directamente en el servidor para ver que error te da.... imprime la sentencia y asegurate que hay los blancos que tiene que haber...

"SELECT SUM(datos.material) as material ".<--este blanco (despues de material y antes de las ")

y cambia de equipo quizas es culpa de MOU
quimfv, gracias por tú interes pero me sigue dando error en la linea 17 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource :
Código PHP:
Ver original
  1. <?php  
  2.  
  3.      $con=mysql_connect ("localhost","xxx","xxx");
  4.      mysql_select_db("Avisoswed",$con);
  5.  
  6. $FInicio= $_POST['FInicio'];
  7. $FFin = $_POST['FFin'];
  8.  
  9. $result = mysql_query("SELECT SUM(datos.material) as material ".
  10.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  11.     "UNION ALL ".
  12.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  13.     "UNION ALL ".
  14.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  15.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';");
  16.  
  17.  
  18.  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  19.  
  20.   {
  21.            $n++;    
  22.            $iva=$row['iva'];
  23.            $total=$row['total'];
  24.            $realizacion=$row['realizacion'];
  25.            $material=$row['material'];
  26.            $id_aviso=$row['id_aviso'];
  27.            $marcas=$row['marcas'];
  28.            echo"<br>";
  29.            echo"<hr>";
  30. echo "<h2 align='center' > FG DESDE  $FInicio HASTA $FFin</h2>"  ;
  31. echo "<h2 align='center' >TOTAL FACTURADO MATERIAL FG </h2>"  ;
  32. echo "<head>
  33.  
  34.  <meta http-equiv='content-type' content='text/html; charset=ISO-8859-1' />
  35.  <title>formulario_tabla</title>
  36.  
  37.  
  38. </head>
  39.  
  40.  
  41.  
  42.  
  43. <body >
  44. <form method='post' action='actualizar_pedido.php'
  45. name='entrada'>
  46.  
  47.  <table border='0' width='100%' style='background-color:#BEBEBE '>
  48.    <tbody>
  49.       <tr>
  50.    
  51.        
  52.    
  53.        <td style='text-align: center;'><small>TOTAL MATERIAL FG </small></td>
  54.        
  55.  
  56.    
  57.    
  58.      </tr>
  59.      <tr>
  60.  
  61.  
  62.    
  63. <td style='width: 120px; text-align: center;'><input
  64. size='10' name='material' value='$material'></td>
  65.  
  66. </tr>
  67.        
  68.  
  69.  
  70.    </tbody>
  71.  </table>
  72.  </form>
  73. </body>
  74. </html>
  75.  
  76.  
  77. ";
  78.  
  79.            }
  80.  
  81.    
  82. ?>
  #6 (permalink)  
Antiguo 10/01/2013, 07:18
Avatar de h2swider  
Fecha de Ingreso: julio-2007
Ubicación: Ciudad de Buenos Aires
Mensajes: 932
Antigüedad: 16 años, 9 meses
Puntos: 194
Respuesta: Consulta en varias tablas.

Hiciste el echo del sql para ver que esta obteniendo como te indicaron?

Código PHP:
Ver original
  1. echo "SELECT SUM(datos.material) as material ".
  2.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  3.     "UNION ALL ".
  4.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  5.     "UNION ALL ".
  6.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  7.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';<br />

Luego pegas la consulta en algun cliente mysql, como phpmyadmin toad... y te fijas si la consulta esta bien.
__________________
Codifica siempre como si la persona que finalmente mantedra tu código sea un psicópata violento que sabe donde vives
  #7 (permalink)  
Antiguo 10/01/2013, 07:49
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Insisto

Cita:
Iniciado por h2swider Ver Mensaje
Hiciste el echo del sql para ver que esta obteniendo como te indicaron?

Código PHP:
Ver original
  1. echo "SELECT SUM(datos.material) as material ".
  2.     "FROM (SELECT f.realizacion,f.material FROM  facturas f ".
  3.     "UNION ALL ".
  4.     "SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
  5.     "UNION ALL ".
  6.     "SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
  7.     "WHERE datos.realizacion BETWEEN '".$FInicio."' AND '".$FFin."';<br />

Luego pegas la consulta en algun cliente mysql, como phpmyadmin toad... y te fijas si la consulta esta bien.
Y esa { de la linea 20 y } de la linea 79 para que son?
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.
  #8 (permalink)  
Antiguo 10/01/2013, 13:15
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Cita:
Iniciado por quimfv Ver Mensaje
Insisto



Y esa { de la linea 20 y } de la linea 79 para que son?
Hola, he hecho la consulta en phpmyadmin y me da el error:

Código HTML:
Ver original
  1. Error
  2.  
  3. consulta SQL: Documentación
  4.  
  5. SELECT SUM( datos.material ) AS material ".     "
  6. FROM (
  7.  
  8. SELECT f.realizacion, f.material
  9. FROM  facturas f ".     "
  10. UNION ALL ".     "
  11. SELECT f11.realizacion, f11.material
  12. FROM  facturas_2011 f11 ".     "
  13. UNION ALL ".     "
  14. SELECT f12.realizacion, f12.material
  15. FROM  facturas_2012 f12
  16. ) AS datos ".     "
  17. WHERE datos.realizacion
  18. BETWEEN '".2011/01/01."'
  19. AND '".2011/12/31."'
  20.  
  21. MySQL ha dicho: Documentación
  22. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '&quot;.
  23. Â  Â  &quot;FROM (SELECT f.realizacion,f.material FROM Â facturas f &quot;.
  24. Â  Â  &quot;UN' at line 1
  #9 (permalink)  
Antiguo 10/01/2013, 14:05
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Parece que me ha funcionado así:

Código PHP:
$result mysql_query("SELECT SUM(datos.material) as material ".
    
"FROM (SELECT f.realizacion,f.material FROM  facturas f ".
    
"UNION ALL ".
    
"SELECT f11.realizacion,f11.material FROM  facturas_2011 f11 ".
    
"UNION ALL ".
    
"SELECT f12.realizacion,f12.material FROM  facturas_2012 f12) as datos ".
    
"WHERE datos.realizacion BETWEEN '$FInicio' AND '$FFin';"); 
Muchas gracias y un saludo

Última edición por satjaen; 10/01/2013 a las 15:30
  #10 (permalink)  
Antiguo 10/01/2013, 15:51
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Estoy intentando sacar otro dato que es el nº de aviso pero no me sale. Por favor me podéis ayudar?

Código PHP:
"SELECT datos.id_aviso "

    
"FROM (SELECT f.id_aviso FROM  facturas f "

    
"UNION ALL ".  

    
"SELECT f11.id_aviso FROM  facturas_2011 f11 ".  

    
"UNION ALL ".  

    
"SELECT f12.id_aviso FROM  facturas_2012 f12) as datos ".  

    
"WHERE datos.id_aviso='3034'"
  #11 (permalink)  
Antiguo 10/01/2013, 17:15
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Ya he podido así:



Código PHP:
SELECT sbc.id_aviso 

 

     FROM 
(

 

   
SELECT g.id_aviso FROM facturas g 

 

   UNION ALL   

 

   SELECT p
.id_aviso FROM facturas_2011 p

 

   UNION ALL

 

   SELECT i
.id_aviso  FROM facturas_2012 i

 

   
) as sbc

 

WHERE sbc
.id_aviso='86'

Gracias
  #12 (permalink)  
Antiguo 10/01/2013, 17:59
Avatar de dashtrash
Colaborador
 
Fecha de Ingreso: abril-2007
Ubicación: Ni en Sevilla,ni en Sanlúcar..qué más da..
Mensajes: 927
Antigüedad: 17 años
Puntos: 270
Respuesta: Consulta en varias tablas.

Mmm..La query que se ha puesto puede ser peligrosa..Yo haría un explain de la query para asegurarme de que no está ocurriendo lo siguiente:
- 1) Se obtienen TODAS las filas de facturas
- 2) Se obtienen TODAS las filas de facturas 2011
- 3) Se obtienen TODAS las filas de facturas 2012
- 4) Se crea 1 resultset con todas esas facturas.
- 5) Se filtra por las fechas seleccionadas.


Es decir, primero, en memoria, se crea una tabla temporal con todas las filas de todas las tablas (que, dependiendo del numero de facturas será más o menos grande), y luego se filtra.
Supón que la query es sólo para fechas dentro de 2011.Se están procesando muchas filas que, a priori, pueden ser descartadas.

No creo que el optimizador de MYSQL mueva las restricciones de fecha, desde la query derivada, a cada una de las queries fuente (las del UNION ALL)

Yo, aunque fuera por seguridad, pondría el where dentro de cada una de las subqueries.Asi mysql filtra antes, y el resulset que maneja es mucho menor.
  #13 (permalink)  
Antiguo 11/01/2013, 02:31
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Cita:
Yo, aunque fuera por seguridad, pondría el where dentro de cada una de las subqueries.Asi mysql filtra antes, y el resulset que maneja es mucho menor.
Era la primera opción propuesta.


Código MySQL:
Ver original
  1. SELECT sbc.id_aviso
  2.      FROM (
  3.    SELECT g.id_aviso FROM facturas g
  4.    UNION ALL  
  5.    SELECT p.id_aviso FROM facturas_2011 p
  6.    UNION ALL
  7.    SELECT i.id_aviso  FROM facturas_2012 i
  8.    ) as sbc
  9. WHERE sbc.id_aviso='86';

El resultado de id_aviso donde id_aviso=86 es 86 luego porque molestar al servidor para preguntar lo que ya sabes...excepto que lo uses para saber si existe o no el aviso 86....

No tendria mas sentido algo asi:

Código MySQL:
Ver original
  1. SELECT sbc.id_aviso,sbc.numFra,sbc.fechaAviso
  2.      FROM (
  3.    SELECT g.id_aviso,g.numFra,g.fechaAviso  FROM facturas g
  4.    UNION ALL  
  5.    SELECT p.id_aviso,p.numFra,p.fechaAviso  FROM facturas_2011 p
  6.    UNION ALL
  7.    SELECT i.id_aviso,i.numFra,i.fechaAviso   FROM facturas_2012 i
  8.    ) as sbc
  9. WHERE sbc.id_aviso='86';

donde obtienes la factura y la fecha del aviso... es un ejemplo quizas sin sentido .... claro...

Cita:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '&quot;.
  &quot;FROM (SELECT f.realizacion,f.material FROM  facturas f &quot;.
  &quot;UN' at line 1
Por cierto el error se produjo porque al copiar pegar te llevaste ruido ... esas  seguramente son los tabuladores.....
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.

Última edición por quimfv; 11/01/2013 a las 02:38
  #14 (permalink)  
Antiguo 11/01/2013, 05:01
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

donde obtienes la factura y la fecha del aviso... es un ejemplo quizas sin sentido .... claro...

quimfv, llevas razón lo que pasa es que no lo puse todo para no complicarlo.
Te lo pongo completo porque me da error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource en esta línea:

Código HTML:
Ver original
  1. $i=mysql_num_rows($r);

Código PHP:
$consulta="(SELECT sbc.id_aviso, sbc.id_factura, sbc.telefonos,sbc.realizacion,sbc.recepcion,sbc.marcas,sbc.name,sbc.apellidos,sbc.calle,sbc.numero,sbc.edificio,sbc.puerta,sbc.piso,sbc.letra,sbc.localidad,sbc.provincia,sbc.dni,sbc.telefonos,sbc.movil,sbc.aparatos,sbc.modelo,sbc.tecnico,sbc.serie,sbc.codigo,sbc.compra,
sbc.sintoma,sbc.recambio,sbc.denominacion,sbc.cantidad,sbc.pvp,sbc.importe ,sbc.recambio2,sbc.denominacion2,sbc.cantidad2,sbc.pvp2,sbc.importe2 ,sbc.recambio3,sbc.denominacion3,sbc.cantidad3,sbc.pvp3,sbc.importe3,sbc.recambio4,sbc.denominacion4,sbc.cantidad4,sbc.pvp4,sbc.importe4 
,sbc.recambio5,sbc.denominacion5,sbc.cantidad5,sbc.pvp5,sbc.importe5,sbc.recambio6,sbc.denominacion6,sbc.cantidad6,sbc.pvp6,sbc.importe6,sbc.recambio7,sbc.denominacion7,sbc.cantidad7,sbc.pvp7,sbc.importe7,sbc.recambio8,sbc.denominacion8,sbc.cantidad8,sbc.pvp8,sbc.importe8
,sbc.recambio9,sbc.denominacion9,sbc.cantidad9,sbc.pvp9,sbc.importe9,sbc.recambio10,sbc.denominacion10,sbc.cantidad10,sbc.pvp10,sbc.importe10,sbc.reparacion,sbc.entrada,sbc.salida,sbc.tiempo,sbc.material,sbc.obra,sbc.desplazamiento,sbc.base,sbc.iva,sbc.total  
FROM (    SELECT g.id_aviso, g.id_factura, g.telefonos,g.realizacion,g.recepcion,g.marcas,g.name,g.apellidos,g.calle,g.numero,g.edificio,g.puerta,g.piso,g.letra,g.localidad,g.provincia,g.dni,g.telefonos,g.movil,g.aparatos,g.modelo,g.tecnico,g.serie,g.codigo,g.compra,
g.sintoma,g.recambio,g.denominacion,g.cantidad,g.pvp,g.importe ,g.recambio2,g.denominacion2,g.cantidad2,g.pvp2,g.importe2 ,g.recambio3,g.denominacion3,g.cantidad3,g.pvp3,g.importe3,g.recambio4,g.denominacion4,g.cantidad4,g.pvp4,g.importe4 
,g.recambio5,g.denominacion5,g.cantidad5,g.pvp5,g.importe5,g.recambio6,g.denominacion6,g.cantidad6,g.pvp6,g.importe6,g.recambio7,g.denominacion7,g.cantidad7,g.pvp7,g.importe7,g.recambio8,g.denominacion8,g.cantidad8,g.pvp8,g.importe8
,g.recambio9,g.denominacion9,g.cantidad9,g.pvp9,g.importe9,g.recambio10,g.denominacion10,g.cantidad10,g.pvp10,g.importe10,g.reparacion,g.entrada,g.salida,g.tiempo,g.material,g.obra,g.desplazamiento,g.base,g.iva,g.total  
FROM facturas g
UNION ALL
SELECT p.id_aviso, p.id_factura, p.telefonos,p.realizacion,p.recepcion,p.marcas,p.name,p.apellidos,p.calle,p.numero,p.edificio,p.puerta,p.piso,p.letra,p.localidad,p.provincia,p.dni,p.telefonos,p.movil,p.aparatos,p.modelo,p.tecnico,p.serie,p.codigo,p.compra,
p.sintoma,p.recambio,p.denominacion,p.cantidad,p.pvp,p.importe ,p.recambio2,p.denominacion2,p.cantidad2,p.pvp2,p.importe2 ,p.recambio3,p.denominacion3,p.cantidad3,p.pvp3,p.importe3,p.recambio4,p.denominacion4,p.cantidad4,p.pvp4,p.importe4 
,p.recambio5,p.denominacion5,p.cantidad5,p.pvp5,p.importe5,p.recambio6,p.denominacion6,p.cantidad6,p.pvp6,p.importe6,p.recambio7,p.denominacion7,p.cantidad7,p.pvp7,p.importe7,p.recambio8,p.denominacion8,p.cantidad8,p.pvp8,p.importe8
,p.recambio9,p.denominacion9,p.cantidad9,p.pvp9,p.importe9,p.recambio10,p.denominacion10,p.cantidad10,p.pvp10,p.importe10,p.reparacion,p.entrada,p.salida,p.tiempo,p.material,p.obra,p.desplazamiento,p.base,p.iva,p.total  
FROM facturas_2011 p
UNION ALL
SELECT i.id_aviso, i.id_factura, i.telefonos,i.realizacion,i.recepcion,i.marcas,i.name,i.apellidos,i.calle,i.numero,i.edificio,i.puerta,i.piso,i.letra,i.localidad,i.provincia,i.dni,i.telefonos,i.movil,i.aparatos,i.modelo,i.tecnico,i.serie,i.codigo,i.compra,
i.sintoma,i.recambio,i.denominacion,i.cantidad,i.pvp,i.importe ,i.recambio2,i.denominacion2,i.cantidad2,i.pvp2,i.importe2 ,i.recambio3,i.denominacion3,i.cantidad3,i.pvp3,i.importe3,i.recambio4,i.denominacion4,i.cantidad4,i.pvp4,i.importe4 
,i.recambio5,i.denominacion5,i.cantidad5,i.pvp5,i.importe5,i.recambio6,i.denominacion6,i.cantidad6,i.pvp6,i.importe6,i.recambio7,i.denominacion7,i.cantidad7,i.pvp7,i.importe7,i.recambio8,i.denominacion8,i.cantidad8,i.pvp8,i.importe8
,i.recambio9,i.denominacion9,i.cantidad9,i.pvp9,i.importe9,i.recambio10,i.denominacion10,i.cantidad10,i.pvp10,i.importe10,i.reparacion,i.entrada,i.salida,i.tiempo,i.material,i.obra,i.desplazamiento,i.base,i.iva,i.total  
FROM facturas_2012 i
) AS sbc
WHERE sbc.id_aviso = '$id_aviso'"
;
$r=mysql_query($consulta);
$i=mysql_num_rows($r); 
Gracias.
  #15 (permalink)  
Antiguo 11/01/2013, 05:18
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Puede ser que el error sea porque yo tenía antes la consulta a una sola tabla y ahora al tener varias tablas los registros les tenga de dar otro nº distinto $registro[0], por cambiar la posicion?:

Código PHP:
Ver original
  1. $consulta="select id_aviso, id_factura,realizacion,recepcion,marcas,name,apellidos,calle,numero,edificio,puerta,piso,letra,localidad,provincia,dni,telefonos,movil,aparatos,modelo,tecnico,serie,codigo,compra,
  2. sintoma,recambio,denominacion,cantidad,pvp,importe ,recambio2,denominacion2,cantidad2,pvp2,importe2 ,recambio3,denominacion3,cantidad3,pvp3,importe3,recambio4,denominacion4,cantidad4,pvp4,importe4
  3. ,recambio5,denominacion5,cantidad5,pvp5,importe5,recambio6,denominacion6,cantidad6,pvp6,importe6,recambio7,denominacion7,cantidad7,pvp7,importe7,recambio8,denominacion8,cantidad8,pvp8,importe8
  4. ,recambio9,denominacion9,cantidad9,pvp9,importe9,recambio10,denominacion10,cantidad10,pvp10,importe10,reparacion,entrada,salida,tiempo,material,obra,desplazamiento,base,iva,total  
  5. from facturas where id_aviso like '$id_aviso' ";

Código PHP:
Ver original
  1. while($registro=mysql_fetch_row($r)){
  2.    
  3.                 $pdf->SetFont('Arial','B',9);
  4.                 $pdf->SetXY(-100,10);$pdf->Cell(85,5,'SERVICIO TÉCNICO OFICIAL',0,0,'C');
  5.                 $pdf->SetFont('Arial','B',8);
  6.                 $pdf->SetXY(-98,26);$pdf->Cell(0,20,'NºAviso',0,5,'L');$pdf->SetXY(-100,33.5);$pdf->SetFont('Arial','I',9);$pdf->Cell(85,5,$registro[0],0,0,'C');
  7.                 $pdf->SetFont('Arial','B',8);$pdf->SetXY(-98,30);$pdf->Cell(0,20,'NºFactura WM2 /',0,5,'L');$pdf->SetXY(-100,37.5);$pdf->SetFont('Arial','I',9);$pdf->Cell(85,5,$registro[1],0,0,'C');
  8.                 $pdf->SetFont('Arial','B',8);$pdf->SetXY(-98,34);$pdf->Cell(0,20,'F.Realización',0,5,'L');$pdf->SetXY(-100,41.5);$pdf->SetFont('Arial','I',9);$pdf->Cell(85,5,$registro[2],0,0,'C');
  9.                 $pdf->SetFont('Arial','B',8);$pdf->SetXY(-98,38);$pdf->Cell(0,20,'F.Recepción',0,5,'L');$pdf->SetXY(-100,45.5);$pdf->SetFont('Arial','I',9);$pdf->Cell(85,5,$registro[3],0,0,'C');
  10.                 $pdf->SetXY(30,51.5);$pdf->Cell(30,5,$registro[5],0,0,'L');$pdf->Cell(85,5,$registro[6],0,0,'L');
  11.                 $pdf->SetXY(30,56.5);$pdf->Cell(100,5,$registro[7],0,0,'L');$pdf->Cell(5,5,$registro[8],0,0,'L');$pdf->Cell(5,5,$registro[9],0,0,'L');$pdf->Cell(10,5,$registro[10],0,0,'L');$pdf->Cell(5,5,$registro[11],0,0,'L');$pdf->Cell(5,5,$registro[12],0,0,'L');
  12.                 $pdf->SetXY(30,61.5);$pdf->Cell(30,5,$registro[13],0,0,'L');$pdf->SetXY(170,61.5);$pdf->Cell(30,5,$registro[14],0,0,'L');
  13.                 $pdf->SetXY(160,51.5);$pdf->Cell(30,5,$registro[15],0,0,'L');
  14.                 $pdf->SetXY(30,66.5);$pdf->Cell(30,5,$registro[16],0,0,'L'); $pdf->SetXY(48,66.5);$pdf->Cell(0,5,'/',0,0,'L');$pdf->SetXY(50,66.5);$pdf->Cell(30,5,$registro[17],0,0,'L');
  15.                 $pdf->SetXY(25,74);$pdf->Cell(30,5,$registro[18],0,0,'L');
  16.                 $pdf->SetXY(25,80.5);$pdf->Cell(30,5,$registro[4],0,0,'L');
  17.                 $pdf->SetXY(25,86.5);$pdf->Cell(30,5,$registro[19],0,0,'L');
  18.                 $pdf->SetXY(25,92.5);$pdf->Cell(30,5,$registro[20],0,0,'L');
  19.                 $pdf->SetXY(118,74);$pdf->Cell(30,5,$registro[21],0,0,'L');
  20.                 $pdf->SetXY(118,80.5);$pdf->Cell(30,5,$registro[22],0,0,'L');
  21.                 $pdf->SetXY(118,86.5);$pdf->Cell(30,5,$registro[23],0,0,'L');
  22.                 $pdf->SetXY(25,104.5);$pdf->Multicell(0,3,$registro[24],0,'J',0);
  23.                 $pdf->SetXY(10,143);$pdf->Cell(30,5,$registro[25],0,0,'L');$pdf->SetXY(50,143);$pdf->Cell(30,5,$registro[26],0,0,'L');$pdf->SetXY(65,143);$pdf->Cell(0,5,$registro[27],0,0,'C');$pdf->SetXY(108,143);$pdf->Cell(0,5,$registro[28],0,0,'C');$pdf->SetXY(175,143);$pdf->Cell(0,5,$registro[29],0,0,'C');
  24.                 $pdf->SetXY(10,148);$pdf->Cell(30,5,$registro[30],0,0,'L');$pdf->SetXY(50,148);$pdf->Cell(30,5,$registro[31],0,0,'L');$pdf->SetXY(65,148);$pdf->Cell(0,5,$registro[32],0,0,'C');$pdf->SetXY(108,148);$pdf->Cell(0,5,$registro[33],0,0,'C');$pdf->SetXY(175,148);$pdf->Cell(0,5,$registro[34],0,0,'C');
  25.                 $pdf->SetXY(10,153);$pdf->Cell(30,5,$registro[35],0,0,'L');$pdf->SetXY(50,153);$pdf->Cell(30,5,$registro[36],0,0,'L');$pdf->SetXY(65,153);$pdf->Cell(0,5,$registro[37],0,0,'C');$pdf->SetXY(108,153);$pdf->Cell(0,5,$registro[38],0,0,'C');$pdf->SetXY(175,153);$pdf->Cell(0,5,$registro[39],0,0,'C');
  26.                 $pdf->SetXY(10,158);$pdf->Cell(30,5,$registro[40],0,0,'L');$pdf->SetXY(50,158);$pdf->Cell(30,5,$registro[41],0,0,'L');$pdf->SetXY(65,158);$pdf->Cell(0,5,$registro[42],0,0,'C');$pdf->SetXY(108,158);$pdf->Cell(0,5,$registro[43],0,0,'C');$pdf->SetXY(175,158);$pdf->Cell(0,5,$registro[44],0,0,'C');
  27.                 $pdf->SetXY(10,163);$pdf->Cell(30,5,$registro[45],0,0,'L');$pdf->SetXY(50,163);$pdf->Cell(30,5,$registro[46],0,0,'L');$pdf->SetXY(65,163);$pdf->Cell(0,5,$registro[47],0,0,'C');$pdf->SetXY(108,163);$pdf->Cell(0,5,$registro[48],0,0,'C');$pdf->SetXY(175,163);$pdf->Cell(0,5,$registro[49],0,0,'C');
  28.                 $pdf->SetXY(10,168);$pdf->Cell(30,5,$registro[50],0,0,'L');$pdf->SetXY(50,168);$pdf->Cell(30,5,$registro[51],0,0,'L');$pdf->SetXY(65,168);$pdf->Cell(0,5,$registro[52],0,0,'C');$pdf->SetXY(108,168);$pdf->Cell(0,5,$registro[53],0,0,'C');$pdf->SetXY(175,168);$pdf->Cell(0,5,$registro[54],0,0,'C');
  29.                 $pdf->SetXY(10,173);$pdf->Cell(30,5,$registro[55],0,0,'L');$pdf->SetXY(50,173);$pdf->Cell(30,5,$registro[56],0,0,'L');$pdf->SetXY(65,173);$pdf->Cell(0,5,$registro[57],0,0,'C');$pdf->SetXY(108,173);$pdf->Cell(0,5,$registro[58],0,0,'C');$pdf->SetXY(175,173);$pdf->Cell(0,5,$registro[59],0,0,'C');
  30.                 $pdf->SetXY(10,178);$pdf->Cell(30,5,$registro[60],0,0,'L');$pdf->SetXY(50,178);$pdf->Cell(30,5,$registro[61],0,0,'L');$pdf->SetXY(65,178);$pdf->Cell(0,5,$registro[62],0,0,'C');$pdf->SetXY(108,178);$pdf->Cell(0,5,$registro[63],0,0,'C');$pdf->SetXY(175,178);$pdf->Cell(0,5,$registro[64],0,0,'C');
  31.                 $pdf->SetXY(10,183);$pdf->Cell(30,5,$registro[65],0,0,'L');$pdf->SetXY(50,183);$pdf->Cell(30,5,$registro[66],0,0,'L');$pdf->SetXY(65,183);$pdf->Cell(0,5,$registro[67],0,0,'C');$pdf->SetXY(108,183);$pdf->Cell(0,5,$registro[68],0,0,'C');$pdf->SetXY(175,183);$pdf->Cell(0,5,$registro[69],0,0,'C');
  32.                 $pdf->SetXY(10,188);$pdf->Cell(30,5,$registro[70],0,0,'L');$pdf->SetXY(50,188);$pdf->Cell(30,5,$registro[71],0,0,'L');$pdf->SetXY(65,188);$pdf->Cell(0,5,$registro[72],0,0,'C');$pdf->SetXY(108,188);$pdf->Cell(0,5,$registro[73],0,0,'C');$pdf->SetXY(175,188);$pdf->Cell(0,5,$registro[74],0,0,'C');
  33.                 $pdf->SetXY(10,213);$pdf->Multicell(60,4,$registro[75],0,'J',0);
  34.                 $pdf->SetXY(15,200);$pdf->Cell(30,5,$registro[76],0,0,'L');
  35.                 $pdf->SetXY(35,200);$pdf->Cell(30,5,$registro[77],0,0,'L');
  36.                 $pdf->SetXY(55,200);$pdf->Cell(30,5,$registro[78],0,0,'L');
  37.                 $pdf->SetXY(174,197);$pdf->Cell(30,5,$registro[79],0,0,'C');
  38.                 $pdf->SetXY(174,202.5);$pdf->Cell(30,5,$registro[80],0,0,'C');
  39.                 $pdf->SetXY(174,208);$pdf->Cell(30,5,$registro[81],0,0,'C');
  40.                 $pdf->SetXY(174,213.5);$pdf->Cell(30,5,$registro[82],0,0,'C');
  41.                 $pdf->SetXY(174,224.5);$pdf->Cell(30,5,$registro[83],0,0,'C');
  42.                 $pdf->SetFont('Arial','I',10);$pdf->SetXY(173,230);$pdf->Cell(30,5,$registro[84],0,0,'C');
  43.              
  44.     }
  #16 (permalink)  
Antiguo 11/01/2013, 06:18
Avatar de dashtrash
Colaborador
 
Fecha de Ingreso: abril-2007
Ubicación: Ni en Sevilla,ni en Sanlúcar..qué más da..
Mensajes: 927
Antigüedad: 17 años
Puntos: 270
Respuesta: Consulta en varias tablas.

Cita:
Iniciado por quimfv Ver Mensaje
Era la primera opción propuesta.
y presentada como equivalente a la segunda, que es la que el op ha terminado usando, y que es mucho más ineficiente...No son para nada equivalentes una cosa y la otra.
  #17 (permalink)  
Antiguo 11/01/2013, 06:29
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Tienes toda la razon...
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.
  #18 (permalink)  
Antiguo 11/01/2013, 06:31
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Consulta en varias tablas.

Cita:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Quiere decir que no se ha creado el result generalmente es por un error en la query... usa la misma tecnica del primer caso, echo "SELECT...." para depurar el error.
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.

Última edición por quimfv; 11/01/2013 a las 07:28
  #19 (permalink)  
Antiguo 11/01/2013, 10:12
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Cita:
Iniciado por quimfv Ver Mensaje
Quiere decir que no se ha creado el result generalmente es por un error en la query... usa la misma tecnica del primer caso, echo "SELECT...." para depurar el error.
Tenia la columna telefonos duplicada. Ya funciona en phpmyadmin pero cuando lo subo al servidor me da el mismo error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in: .
Código HTML:
Ver original
  1. $i=mysql_num_rows($r);
  #20 (permalink)  
Antiguo 12/01/2013, 12:28
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: Consulta en varias tablas.

Ok, resuelto.

Etiquetas: mysql, select, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:59.