Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154

    [mysql] tabella view con dati di 2 tabelle

    Ciao a tutti. Avrei bisogno di creare una tabella di tipo view che mi unisca i dati di due tabelle diverse.
    Le 2 tabelle da unire hanno anche dei campi duplicati.
    La tabella di tipo view la chiamo "all_comments", mentre le 2 tabelle da unire si chiamano "commenti_ricette" e "commenti_news".
    In pratica avrei bisogno che in ogni singola riga della tabella view andassero i dati di 2 righe delle tabelle
    "commenti_ricette" e "commenti_news".
    Ho provato così:

    codice:
    CREATE VIEW all_comments (id_x_link, data_commento, nome_utente, email_utente, ip_utente, testo_commento) AS SELECT commenti_news.id_commento_news || commenti_ricette.id_commento, commenti_news.data_commento_news || commenti_ricette.data_commento, commenti_news.nome_utente || commenti_ricette.nome_utente, commenti_news.email_utente || commenti_ricette.email_utente, commenti_news.ip_address || commenti_ricette.ip_adress_ricette, commenti_news.commento_news || commenti_ricette.commento_ricetta FROM commenti_news, commenti_ricette
    E poi così

    codice:
    CREATE ALGORITHM = TEMPTABLE VIEW all_comments (id_x_link, data_commento, nome_utente, 
    email_utente, ip_utente, testo_commento) AS SELECT commenti_news.id_commento_news || commenti_ricette.id_commento, commenti_news.data_commento_news || commenti_ricette.data_commento, commenti_news.nome_utente || commenti_ricette.nome_utente, commenti_news.email_utente || commenti_ricette.email_utente, commenti_news.ip_address || commenti_ricette.ip_adress_ricette, commenti_news.commento_news || commenti_ricette.commento_ricetta FROM commenti_news, commenti_ricette
    In entrambi i casi la query viene eseguita, ma viene fuori un risultato quasi vuoto, solo con alcuni 0
    e 1 in alcuni campi.

  2. #2
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Prova questa query e dimmi se è quello che volevi ottenere.

    codice:
    select id_commento,
    max(case when tab = 1 then data_commento else null end) as data_news,
    max(case when tab = 2 then data_commento else null end) as data_ricette,
    max(case when tab = 1 then nome_utente else null end) as nome_news,
    max(case when tab = 2 then nome_utente else null end) as nome_ricette,
    max(case when tab = 1 then email_utente else null end) as mail_news,
    max(case when tab = 2 then email_utente else null end) as mail_ricette,
    max(case when tab = 1 then ip_address else null end) as ip_news,
    max(case when tab = 2 then ip_address else null end) as ip_ricette,
    max(case when tab = 1 then commento_news else null end) as commento_news,
    max(case when tab = 2 then commento_news else null end) as commento_ricette
    from (
    select *,1 as tab from commenti_news
    union 
    select *,2 as tab from commenti_ricette
    ) as tab
    group by id_commento

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154
    Ciao, appena posso provo la query e ti faccio sapere

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154
    Ciao nicola75ss, ho provato la query ma non funziona. Ho adattato i nomi alle mie tabelle e la ho provata ma mi restituisce questo errore: #1054 - Unknown column 'id_commento' in 'field list'

  5. #5
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    prova a mettere id_commento_news.

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154
    Niente da fare. Ti posto la query che ho usato:

    codice:
    SELECT id_commento_news,
    max(case when tab = 1 then data_commento else null end) as data_commento_news,
    max(case when tab = 2 then data_commento else null end) as data_commento,
    max(case when tab = 1 then nome_utente else null end) as nome_utente,
    max(case when tab = 2 then nome_utente else null end) as nome_utente,
    max(case when tab = 1 then email_utente else null end) as email_utente,
    max(case when tab = 2 then email_utente else null end) as email_utente,
    max(case when tab = 1 then ip_address else null end) as ip_address,
    max(case when tab = 2 then ip_address else null end) as ip_adress_ricette,
    max(case when tab = 1 then commento_news else null end) as commento_news,
    max(case when tab = 2 then commento_news else null end) as commento_ricetta
    from (
    select data_commento_news, nome_utente, email_utente, ip_address, commento_news,1 as tab from commenti_news
    union 
    select data_commento, nome_utente, email_utente, ip_adress_ricette, commento_ricetta,2 as tab from commenti_ricette
    ) as tab
    group by id_commento_news

  7. #7
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Nella mia query, in fase di union, avevo messo l'asterisco per recuperare tutti i campi da entrambe le tabelle. Se tu li specifichi uno ad uno, partendo da data commento, è ovvio che la query esterna non trovi il campo che fa riferimento all'id visto che non l'hai selezionato. Quindi o rimetti l'asterisco come avevo fatto io, oppure specifichi anche il campo relativo all'id nelle due tabelle interne messe in unione.

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154
    Avevo provato lasciando l'asterisco che mi selezionava tutto, ma mi dava un altro errore, ora non ricordo quale(sto facendo un milione di prove). Cmq era qualcosa tipo che non trovata le tabelle o non c'era corrispondenza.... Ora cmq riprovo

  9. #9
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    La union richiede che venga selezionato lo stesso numero di campi dalle tabelle. Dal tuo post iniziale mi era parso di capire che la situazione fosse quella. Se invece il numero dei campi non coincide, allora devi provvedere a selezionarli esplicitamente.

  10. #10
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    154
    Purtroppo niente.
    Query:
    codice:
    SELECT id_commento_news,
    max(case when tab = 1 then data_commento else null end) as data_commento_news,
    max(case when tab = 2 then data_commento else null end) as data_commento,
    max(case when tab = 1 then nome_utente else null end) as nome_utente,
    max(case when tab = 2 then nome_utente else null end) as nome_utente,
    max(case when tab = 1 then email_utente else null end) as email_utente,
    max(case when tab = 2 then email_utente else null end) as email_utente,
    max(case when tab = 1 then ip_address else null end) as ip_address,
    max(case when tab = 2 then ip_address else null end) as ip_adress_ricette,
    max(case when tab = 1 then commento_news else null end) as commento_news,
    max(case when tab = 2 then commento_news else null end) as commento_ricetta
    from (
    select id_commento_news, data_commento_news, nome_utente, email_utente, ip_address, commento_news,1 as tab from commenti_news
    union 
    select id_commento, data_commento, nome_utente, email_utente, ip_adress_ricette, commento_ricetta,2 as tab from commenti_ricette
    ) as tab
    group by id_commento_news
    Errore:#1054 - Unknown column 'data_commento' in 'field list' Ma è strano perchè quella tabella esiste.

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.