Visualizzazione dei risultati da 1 a 7 su 7

Discussione: Query sql difficile

  1. #1
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797

    Query sql difficile

    Salve gente.
    mi serve un aiuto, allora:
    ho queste tabelle:

    questionario_risposta,
    contiene id della domanda, della risposta.
    il questionario a cui appartengono quelle domande/risposte
    e che utente ha risposto
    codice:
    create table qo_questionario_risposta (
    id_questionario_risposta serial not null,
    id_questionario integer not null references qo_questionario(id_questionario),
    id_domanda integer not null references qo_domanda(id_domanda),
    id_risposta integer not null references qo_risposta(id_risposta),
    id_utente integer not null references contatto(id_contatto) ,
    primary key (
    id_questionario_risposta
    ));

    il testo della domanda
    [CODE]
    create table qo_testo_domanda (
    id_lingua integer references qo_lingua (id_lingua),
    testo text,
    id_domanda integer references qo_domanda(id_domanda),
    primary key (
    id_lingua,
    id_domanda
    ));

    il testo della risposta
    codice:
    create table qo_testo_risposta (
    id_lingua integer references qo_lingua (id_lingua),
    id_risposta integer references qo_risposta(id_risposta),
    testo text,
    primary key (
    id_lingua,
    id_risposta
    ));

    ora dovrei fare una query che mi restituisce

    TESTO DOMANDA
    - TESTI RISPOSTA DATI (cioè tutte le rispsote a quella domanda)

    con input l'id questionario e id utente.

    qualcuno sa darmi una mano?
    praticamnet è una sorta di report delle risposte.
    grazie

  2. #2
    prova così

    select d.testo, r.testo from qo_questionario_risposta q join qo_testo_domanda d on q.id_domanda=p.id_domanda join qo_testo_risposta on q.id_risposta=p.id_risposta WHERE q.idquestionario="val" e q.id_utente="val"



  3. #3
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    quindi se ho capito il risultato dovrebbe essere +/- questo:

    Domanda
    risposta utente1 con id
    risposta utente 2 con id
    risposta utente3 con id

    se è questo che vupoi, struttura le tabelle in questo modo per renderti la vita + semplice:

    tbl_domande
    ID_domanda P:K
    domanda text


    tbl_risposte
    ID_risposta PK
    risposta text
    id_domanda FK
    id_utente FK


    tbl_utente

    ID_utente PK
    utente text


    questa la query



    Codice PHP:
     select c.domanda GROUP_CONCAT(CONCAT_WS(''p.risposta,u.utente,i.ID_utente)ORDER by ID_utente SEPARATOR '
    '
    risposte FROM tbl_risposte,tbl_utente LEFT JOIN tbl_domande ON id_domanda=ID_domanda and id_utente=ID_utente GROUP by ID_domanda ORDER by c.domanda 
    per un problema simile al tuo mi è risultato piu comodo questo

  4. #4
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    allora nn è proprio così ma mia struttira
    ho
    Domanda
    id_domanda
    testo

    risposta
    id_risposta
    testo

    riposta_domanda
    id_domanda
    id_risposta
    id_utente
    id_questionario

    presi come dati id_utente e id_questionario devo ricavarmi quello che ha scritto
    Lowenthal.

    potresti spiegarmi la tua query cosa fa di preciso?vorrei capire
    grazie

  5. #5
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    a cmq a me servirebbe l'sql puro dato che poi la uso in java la query.
    grazie

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    442
    avendo le seguenti tabelle

    domanda
    id_domanda
    testo

    risposta
    id_risposta
    testo

    riposta_domanda
    id_domanda
    id_risposta
    id_utente
    id_questionario

    utente
    ID_utente
    utente

    ti mostro sql:


    Codice PHP:
     SELECT domanda.testorisposta.testoutente.utente
    FROM utente INNER JOIN 
    (risposta INNER JOIN (domanda INNER JOIN risposta_domanda ON domanda.ID_domanda=risposta_domanda.id_domandaON risposta.ID_risposta=risposta_domanda.id_rispostaON utente.ID_utente=risposta_domanda.id_utente
    questo ti mostra
    domanda 1 risposta utente1
    domanda1 risposta utente2
    domanda1 risposta utente 3
    domanda 2 risposta utente1
    domanda2 risposta utente2
    domanda2 risposta utente 3
    poi per

    Domanda
    risposta utente1
    risposta utente2
    risposta utente3
    Domanda
    risposta utente1
    risposta utente2
    risposta utente3


    ci si affida ai report

    se posso darti un consiglio nel modo come mi hai spiegato te secondo me fare una tabella in piu (mi riferisco a risposta_domanda) ti complichi e basta .secondo me era meglio come ti avevo scritto nel messaggio precedente ossia


    Codice PHP:
    domanda
    ID_domanda P
    :K
    testo text


    risposta
    ID_risposta PK
    testo text
    id_domanda FK
    id_utente FK


    utente

    ID_utente PK
    utente text 

    con la query

    select domanda.testo GROUP_CONCAT(CONCAT_WS(''risposta.testo,utente.ute nte)ORDER by utente.ID_utente SEPARATOR '
    ') risposta FROM risposta,utente LEFT JOIN domanda ON id_domanda=ID_domanda and id_utente=ID_utente GROUP by ID_domanda ORDER by domanda.testo

    saluti luigi

  7. #7
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    è che le tabelle mi servono così per una questione di multilingua che è un po' un casino spiegare.
    grazie, dopo provo

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.