Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 36
  1. #1
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802

    Query su diverse tabelle

    Ciao a tutti,
    ho un problema su una query un po complessa e le join non sono il mio forte quindi chiedo a voi.

    Devo fare una select da tabella1 e tabella2 in base all'ID presente su tabella3 per poi fare una seconda select per estrarre i nomi a seconda degli ID estrapolati precedentemente. E' un casino quindi vi faccio un esempio per chiarire.

    Tabella1:
    IDtabella1
    IDa
    IDb

    Tabella2:
    IDtabella2
    IDa
    IDc

    Tabella3:
    IDtabella3
    IDa

    Io devo fare la select per strarre tabella1.IDb e tabella2.IDc dove IDa della tabella1 e tabella2 corrisponde a IDa della tabella3.

    Poi ho Tabella4 che contiene IDa e un campo nome:

    Tabella4
    IDa
    Nome

    Qui dovrei fare la select per strapolare il campo Nome a seconda degli IDa estrapolati nella select precedente.

    Non so se è chiaro ma spero di si. Potete darmi una mano? Mi si sta fondendo il cervello a forza di cercare una soluzione. Credo che la chiave sia nel SQL ma deov ancora fare pratica e per le select un po particolari ho ancora difficoltà.

    Garzie mille per l'aiuto.
    www.beppegrillo.it
    Il blog di Beppe!!

  2. #2
    Può andare ...?

    codice:
    select Nome from tabella4
    where IDa IN (select IDa from tabella3
    where IDa IN ((Select IDb from tabella1 UNION select idc from tabella2)))

  3. #3
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    Mi da errore di sintassi.
    Magari sono io che sbaglio, quindi vi metto qui sotto l'esempio corretto con il nome e i campi delle tabelle reali.


    Tabella "tabella1_2"
    IDstrumenti = chiave
    IDtrattamento
    .
    .
    .
    .

    Tabella "rifstrutture"
    IDrif = chiave
    IDstruttura
    IDtrattamento

    Tabella "rifstruttureesterne"
    IDrifesterna = chiave
    IDstrutture
    IDtrattamento

    Tabella "strutture"
    IDstruttura
    Nome
    .
    .
    .
    .


    Bene, vorrei estrapolare i dati "rifstrutture.IDstruttura" e "rifstruttureesterne.IDstrutture" in base a "IDtrattamento" di "tabella1_2" selezionato in precedenza. Una volta estrapolati "IDstruttura" e "IDstrutture" devo utilizzarli per ricavare "Nome" dalla tabella "Strutture".

    :master:
    www.beppegrillo.it
    Il blog di Beppe!!

  4. #4
    In questo caso se devi estrarre in base all'IDTrattamento,
    non ti serve la tabella1_2. La query potrebbe essere questa, ovviamento sostituisci VALORE con l'ID del trattamento che stai cercando

    codice:
    select Nome from Strutture
    where IDStruttura IN (select IDStruttura from rifstrutture where IDTrattamento = VALORE)
    AND IDStruttura IN (select IDStrutture from rifstruttureesterne where IDTrattamento = VALORE)

  5. #5
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    Mi da errore ancora:

    select struttura from strutture where IDStruttura IN (select IDStruttura from rifstruttura where IDTrattamento = '"&Cint(IDtratt)&"') AND IDStruttura IN (select IDStrutture from rifstruttureesterne where IDTrattamento = '"&Cint(IDtratt)&"')



    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 'select IDStrutt(

    /modtab1-2.asp, line 343


    Perchè?
    www.beppegrillo.it
    Il blog di Beppe!!

  6. #6
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    Specifico che il DB è MySQL.

    Altra cosa, per dirla tutta, dovrei effettuare anche un altro controllo mentre estrapolo i dati:

    Tabella "tabella1_2"
    IDstrumenti = chiave
    IDtrattamento
    .
    .
    .
    .

    Tabella "rifstrutture"
    IDrif = chiave
    IDstruttura
    IDtrattamento

    Tabella "rifstruttureesterne"
    IDrifesterna = chiave
    IDstrutture
    IDtrattamento

    Tabella "strutture"
    IDstruttura
    Nome
    .
    .
    .
    .

    Tabella "rifsede"
    IDrif = chiave
    IDstrumenti
    IDstruttura


    Bene, aggiornando il tutto, vorrei estrapolare i dati "rifstrutture.IDstruttura" e "rifstruttureesterne.IDstrutture" in base a "IDtrattamento" di "tabella1_2" selezionato in precedenza. Una volta estrapolati "IDstruttura" e "IDstrutture" devo utilizzarli per ricavare "Nome" dalla tabella "Strutture" e flaggarlo in base a "IDstruttura" della tabella rifsede, ovvero flaggare quei dati estrapolati dalla tabella "Strutture" il cui campo "IDstruttura" coincide con "IDstruttura" della tabella "rifsede".


    Ok, è un po incasinato ma leggendolo attentamente si dovrebbe capire il significato. Datemi una mano perchè sono fermo in questo piunto da parecchio e non riesco a uscirne.
    www.beppegrillo.it
    Il blog di Beppe!!

  7. #7
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    UP! Chiedo troppo?
    www.beppegrillo.it
    Il blog di Beppe!!

  8. #8
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    Ho provato con questo codice SQL usando la INNER JOIN ma non mi fa visualizzare nulla:

    select struttura from strutture INNER JOIN rifstruttura, rifstruttureesterne ON strutture.IDstruttura = rifstruttura.IDstruttura AND strutture.IDstruttura = rifstruttureesterne.IDstrutture AND rifstruttura.IDtrattamento LIKE '91' AND rifstruttureesterne.IDtrattamento LIKE '91'


    I due numeri inseriti li ho messi manualmente per testare la funzionalità ma non mi fa nessuna query nonostante non venga restituito alcun errore. Probabilmente sbaglio la struttura della select ma non so come diavolo farla e nessuno è in grado darmi una mano, per cui chi ha le capacità di aiutarmi faccia un opera buona plz.


    Ho anche fatto la seguente prova senza alcun risultato:

    select struttura from strutture INNER JOIN rifstruttura, rifstruttureesterne ON strutture.IDstruttura = rifstruttura.IDstruttura AND strutture.IDstruttura = rifstruttureesterne.IDstrutture WHERE rifsruttura.IDtrattamento LIKE '91' AND rifstruttureesterne.IDtrattamento LIKE '91'



    www.beppegrillo.it
    Il blog di Beppe!!

  9. #9
    Utente di HTML.it L'avatar di colde
    Registrato dal
    Feb 2001
    Messaggi
    1,802
    Urka quanta gente.

    Con tutti gli utenti che ci sono ci sarà un guru dell'SQL?

    Mha!
    www.beppegrillo.it
    Il blog di Beppe!!

  10. #10
    Hai provato a farti stampare con un bel Response.write la query
    e poi provarla in mysql senza passare da asp e vedere dove ti segnala l'errore, su che punto della stringa SQL.

    Ti parlo di questa stringa :

    codice:
    select struttura from strutture where IDStruttura IN (select IDStruttura from rifstruttura where IDTrattamento = '"&Cint(IDtratt)&"') AND IDStruttura IN (select IDStrutture from rifstruttureesterne where IDTrattamento = '"&Cint(IDtratt)&"')

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 © 2025 vBulletin Solutions, Inc. All rights reserved.