Guest User

Untitled

a guest
Jun 26th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. create table tipo_producto(
  2. id_tipo serial,
  3. nombre character varying(20),
  4. activo boolean default False,
  5. primary key(id_tipo)
  6. );
  7.  
  8. create table producto(
  9. id_producto serial,
  10. id_tipo integer,
  11. nombre character varying(20),
  12. descripcion text,
  13. precio integer default 0,
  14. stock integer default 0,
  15. activo boolean default False,
  16. primary key(id_producto),
  17. Foreign key(id_tipo) references tipo_producto(id_tipo)
  18. );
  19.  
  20. create table imagen(
  21. id_imagen serial,
  22. id_producto integer,
  23. nombre character varying(100),
  24. alto integer,
  25. ancho integer,
  26. orden integer default 0,
  27. activo boolean default False,
  28. primary key(id_imagen),
  29. Foreign key(id_producto) references producto(id_producto)
  30. );
  31.  
  32. SELECT i.nombre, p.nombre, tp.nombre
  33. FROM producto p
  34. INNER JOIN tipo_producto tp ON p.id_tipo = tp.id_tipo
  35. LEFT JOIN imagen i ON p.id_producto = i.id_producto AND orden = 1
  36.  
  37. SELECT i.nombre, p.nombre, tp.nombre
  38. FROM producto p
  39. LEFT OUTER JOIN tipo_producto tp ON p.id_tipo = tp.id_tipo
  40. LEFT OUTER JOIN imagen i ON p.id_producto = i.id_producto
  41.  
  42. Producto.objects.filter(imagen__orden=1).values('imagen__nombre','nombre','id_tipo_id__nombre')
  43.  
  44. SELECT i.nombre, p.nombre, tp.nombre
  45. FROM producto p
  46. INNER JOIN imagen i ON p.id_producto = i.id_producto
  47. LEFT OUTER JOIN tipo_producto tp ON p.id_tipo = tp.id_tipo
  48. WHERE i.orden = 1
Add Comment
Please, Sign In to add comment