Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente di HTML.it L'avatar di mahimo
    Registrato dal
    Feb 2003
    Messaggi
    251

    select su più tabelle (join?)

    in un db mysql ho due tabelle così strutturate:

    elenco_gallery
    id_gallery
    nome_gallery
    id_tipologia_gallery

    mentre qui vengono memorizzati tutti i percorsi relativi alle immagini
    immagini
    id_immagine
    id_gallery
    img_piccola
    img_grande

    in "elenco_gallery.asp" scorro la tabella elenco_gallery e visualizzo l'id della gallery, il nome e la tipologia. ora vorrei visualizzare anche il numero delle foto presenti per ogni gallery.
    per fare ciò mi basterebbe far scorrere la tabella "immagini" e fare il conteggio di quanti record hanno lo stesso id gallery, giusto?
    ho letto che probabilmente in questo caso devo fare una select "join", ma non sono riuscito a capire come eseguirla. riuscite a spiegarmene il concetto?

  2. #2
    Vediamo un po...
    codice:
    set conn = ...
    conn.open ...
    
    sql = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
            "elenco_gallery.id_gallery, " &_
            "elenco_gallery.nome_gallery, " &_
            "elenco_gallery.id_tipologia_gallery " &_
            "FROM elenco_gallery " &_
            "INNER JOIN immagini " &_
            "ON elenco_gallery.id_gallery = immagini.id_gallery" &_
            "ORDER BY elenco_gallery.nome_gallery"
    
    set rs = conn.execute(sql)
    
       if not rs.eof then
          do until rs.eof
             response.write rs("nome_gallery") & "(" & rs("totImg") & ")
    "
          rs.moveNext
          loop
       end if
    
    rs.close
    set rs = nothing
    %>

  3. #3
    Utente di HTML.it L'avatar di mahimo
    Registrato dal
    Feb 2003
    Messaggi
    251
    'aspita... grazie per la prontezza..
    ora me lo studio bene e lo provo

  4. #4
    Originariamente inviato da mahimo
    'aspita... grazie per la prontezza..
    ora me lo studio bene e lo provo
    Spero che funzioni...

  5. #5
    Utente di HTML.it L'avatar di mahimo
    Registrato dal
    Feb 2003
    Messaggi
    251
    non funzia, ho provato comunque ad adattarlo al mio codice e ho riguardato anche sul manuale... la sintassi però mi sembra corretta..... :master:

    codice:
    
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery" &_
             "ORDER BY elenco_gallery.nome_gallery"
    		 
    Set rs=Server.CreateObject("ADODB.recordset")
    rs.Open strSQL, objcon
    while not rs.EOF
    
    response.write rs("nome_gallery") & "(" & rs("totImg") & ")</br>"
    
    rs.moveNext
    
    wend
    
    rs.close
    set rs = nothing
    
    objcon.close
    Set objcon = Nothing
    l'errore restituito è:
    Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

    [MySQL][ODBC 3.51 Driver][mysqld-5.0.27-standard-log]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 'BY elenco_gallery.nome_gallery' at line 1

    /public/new/elenco.asp, line 21

    e la linea 21 è quella con l'esecuzione della stringa sql

  6. #6
    Prova adesso.
    Mancava uno spazio alla fine della penultima riga.
    codice:
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery " &_
             "ORDER BY elenco_gallery.nome_gallery"

  7. #7
    Utente di HTML.it L'avatar di mahimo
    Registrato dal
    Feb 2003
    Messaggi
    251
    con questo codice non funziona ancora :master:

    codice:
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery " &_
             "ORDER BY elenco_gallery.nome_gallery"
    		 
    Set rs=Server.CreateObject("ADODB.recordset")
    rs.Open strSQL, objcon
    while not rs.EOF
    
    response.write rs("nome_gallery") & "(" & rs("totImg") & ")</br>"
    
    rs.moveNext
    
    wend
    
    rs.close
    set rs = nothing
    
    objcon.close
    Set objcon = Nothing
    ora l'errore però è:
    Microsoft OLE DB Provider for ODBC Drivers error '80040e31'

    [MySQL][ODBC 3.51 Driver][mysqld-5.0.27-standard-log]Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

    /public/new/elenco.asp, line 21

  8. #8
    codice:
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery " &_
             "GROUP BY totImg " &_
             "ORDER BY elenco_gallery.nome_gallery"
    Penso che dia qualche altro errore adesso...

  9. #9
    Utente di HTML.it L'avatar di mahimo
    Registrato dal
    Feb 2003
    Messaggi
    251
    codice:
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery " &_
             "GROUP BY id_gallery  " &_
             "ORDER BY elenco_gallery.nome_gallery"
    funzia! però cambiando il "group by" con id_gallery anzichè totImg
    che dire.. ti ringrazio. ora sinceramente ho le idee un po confuse... ma sono sicuro che se lo riguardo domattina tutto mi sembrerà più chiaro

    ciao e grazie mille!

  10. #10
    A me ha funzionato in questo modo:
    codice:
    strSQL = "SELECT COUNT(immagini.id_immagine) AS totImg, " &_
             "elenco_gallery.id_gallery, " &_
             "elenco_gallery.nome_gallery, " &_
             "elenco_gallery.id_tipologia_gallery " &_
             "FROM elenco_gallery " &_
             "INNER JOIN immagini " &_
             "ON elenco_gallery.id_gallery = immagini.id_gallery " &_
             "GROUP BY elenco_gallery.id_gallery, nome_gallery, id_tipologia_gallery  " &_
             "ORDER BY elenco_gallery.nome_gallery"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.