duda sql

01/08/2003 - 20:59 por Oterox | Informe spam
Hola a todos.

Tengo 3 tablas con una relacion muchos-muchos:

libros ( id_libro , titulo , autor )
1 tit1 yo
2 tit2 tu

categorias ( idCategoria , nombre )
1 asp
2 ado
3 xml

librosCategorias ( idCategoria , idLibro)
1 1
2 1
1 2
3 2

Como seria la consulta sql para mostrar algo asi:

id_libro titulo Categorias
1 tit1 asp, ado
2 tit2 asp, xml

Muchas gracias a todos ;-)

Preguntas similare

Leer las respuestas

#1 Wilfredo Burgos
01/08/2003 - 22:32 | Informe spam
Te verifico y te dajaré saber.


"Oterox" wrote in message
news:O$
Hola a todos.

Tengo 3 tablas con una relacion muchos-muchos:

libros ( id_libro , titulo , autor )
1 tit1 yo
2 tit2 tu

categorias ( idCategoria , nombre )
1 asp
2 ado
3 xml

librosCategorias ( idCategoria , idLibro)
1 1
2 1
1 2
3 2

Como seria la consulta sql para mostrar algo asi:

id_libro titulo Categorias
1 tit1 asp, ado
2 tit2 asp, xml

Muchas gracias a todos ;-)


Respuesta Responder a este mensaje
#2 Liliana Sorrentino
04/08/2003 - 17:44 | Informe spam
SELECT id_libro, titulo,
MAX(CASE categorias.idCategoria WHEN 1 THEN RTRIM(nombre) + ';' ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 2 THEN RTRIM(nombre) + ';' ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 3 THEN RTRIM(nombre) + ';' ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 4 THEN RTRIM(nombre) + ';' ELSE
'' END)
FROM librosCategorias
INNER JOIN categorias ON categorias.idcategoria librosCategorias.idcategoria
INNER JOIN libros on idLibro = id_libro
GROUP BY id_libro, titulo
ORDER BY id_libro

Sobre Referencias Cruzadas, encontrarás información útil en las
sig.direcciones:

If you know in advance the number of columns in the result set, try:
http://support.microsoft.com/defaul...S;q175574.

If you have a variable number of columns, try:
http://www.sqlmag.com/Articles/Index.cfm?ArticleID608

If you want some interesting utilities to do it for you:
http://www.ag-software.com/AGS/xp ags crosstab.asp

RAC at:
http://www.rac4sql.net/onlinehelp.asp?topic(5

Saludos... Liliana.

"Oterox" escribió en el mensaje
news:O$
Hola a todos.

Tengo 3 tablas con una relacion muchos-muchos:

libros ( id_libro , titulo , autor )
1 tit1 yo
2 tit2 tu

categorias ( idCategoria , nombre )
1 asp
2 ado
3 xml

librosCategorias ( idCategoria , idLibro)
1 1
2 1
1 2
3 2

Como seria la consulta sql para mostrar algo asi:

id_libro titulo Categorias
1 tit1 asp, ado
2 tit2 asp, xml

Muchas gracias a todos ;-)


Respuesta Responder a este mensaje
#3 Oterox
04/08/2003 - 21:12 | Informe spam
Muchas gracias liliana.
Ese sql me funciona perfectamente pero me vale solo para cuando hay 4
categorias y en realidad deberia haber ilimitadas categorias.
Como se haria para tener ilimitadas categorias?

Muchas gracias



"Liliana Sorrentino" escribió en el mensaje
news:

SELECT id_libro, titulo,
MAX(CASE categorias.idCategoria WHEN 1 THEN RTRIM(nombre) + ';'


ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 2 THEN RTRIM(nombre) + ';'


ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 3 THEN RTRIM(nombre) + ';'


ELSE
'' END) +
MAX(CASE categorias.idCategoria WHEN 4 THEN RTRIM(nombre) + ';'


ELSE
'' END)
FROM librosCategorias
INNER JOIN categorias ON categorias.idcategoria > librosCategorias.idcategoria
INNER JOIN libros on idLibro = id_libro
GROUP BY id_libro, titulo
ORDER BY id_libro

Sobre Referencias Cruzadas, encontrarás información útil en las
sig.direcciones:

If you know in advance the number of columns in the result set, try:
http://support.microsoft.com/defaul...S;q175574.

If you have a variable number of columns, try:
http://www.sqlmag.com/Articles/Index.cfm?ArticleID608

If you want some interesting utilities to do it for you:
http://www.ag-software.com/AGS/xp ags crosstab.asp

RAC at:
http://www.rac4sql.net/onlinehelp.asp?topic(5

Saludos... Liliana.

"Oterox" escribió en el mensaje
news:O$
> Hola a todos.
>
> Tengo 3 tablas con una relacion muchos-muchos:
>
> libros ( id_libro , titulo , autor )
> 1 tit1 yo
> 2 tit2 tu
>
> categorias ( idCategoria , nombre )
> 1 asp
> 2 ado
> 3 xml
>
> librosCategorias ( idCategoria , idLibro)
> 1 1
> 2 1
> 1 2
> 3 2
>
> Como seria la consulta sql para mostrar algo asi:
>
> id_libro titulo Categorias
> 1 tit1 asp, ado
> 2 tit2 asp, xml
>
> Muchas gracias a todos ;-)
>
>


Respuesta Responder a este mensaje
#4 Liliana Sorrentino
05/08/2003 - 13:48 | Informe spam
Por supuesto el select que te envié es para que tengas como referencia y
puedas buscar tu propia solución. Pero al final del mismo, te mando unos
links entre los cuales está el que resuelve tu problema, yo lo usé varias
veces y funciona perfecto. Si tenés problemas con él, volvemos a
comunicarnos y te ayudo a encontrar la salida.

> If you have a variable number of columns, try:
> http://www.sqlmag.com/Articles/Index.cfm?ArticleID608



Saludos... Liliana.

"Oterox" escribió en el mensaje
news:#
Muchas gracias liliana.
Ese sql me funciona perfectamente pero me vale solo para cuando hay 4
categorias y en realidad deberia haber ilimitadas categorias.
Como se haria para tener ilimitadas categorias?

Muchas gracias



"Liliana Sorrentino" escribió en el


mensaje
news:
>
> SELECT id_libro, titulo,
> MAX(CASE categorias.idCategoria WHEN 1 THEN RTRIM(nombre) + ';'
ELSE
> '' END) +
> MAX(CASE categorias.idCategoria WHEN 2 THEN RTRIM(nombre) + ';'
ELSE
> '' END) +
> MAX(CASE categorias.idCategoria WHEN 3 THEN RTRIM(nombre) + ';'
ELSE
> '' END) +
> MAX(CASE categorias.idCategoria WHEN 4 THEN RTRIM(nombre) + ';'
ELSE
> '' END)
> FROM librosCategorias
> INNER JOIN categorias ON categorias.idcategoria > > librosCategorias.idcategoria
> INNER JOIN libros on idLibro = id_libro
> GROUP BY id_libro, titulo
> ORDER BY id_libro
>
> Sobre Referencias Cruzadas, encontrarás información útil en las
> sig.direcciones:
>
> If you know in advance the number of columns in the result set, try:
> http://support.microsoft.com/defaul...S;q175574.
>
> If you have a variable number of columns, try:
> http://www.sqlmag.com/Articles/Index.cfm?ArticleID608
>
> If you want some interesting utilities to do it for you:
> http://www.ag-software.com/AGS/xp ags crosstab.asp
>
> RAC at:
> http://www.rac4sql.net/onlinehelp.asp?topic(5
>
> Saludos... Liliana.
>
> "Oterox" escribió en el mensaje
> news:O$
> > Hola a todos.
> >
> > Tengo 3 tablas con una relacion muchos-muchos:
> >
> > libros ( id_libro , titulo , autor )
> > 1 tit1 yo
> > 2 tit2 tu
> >
> > categorias ( idCategoria , nombre )
> > 1 asp
> > 2 ado
> > 3 xml
> >
> > librosCategorias ( idCategoria , idLibro)
> > 1 1
> > 2 1
> > 1 2
> > 3 2
> >
> > Como seria la consulta sql para mostrar algo asi:
> >
> > id_libro titulo Categorias
> > 1 tit1 asp, ado
> > 2 tit2 asp, xml
> >
> > Muchas gracias a todos ;-)
> >
> >
>
>


email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida