Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    541

    Query difficile di ordinamento

    Ho un problema con una query un po' complicata. Mi trovo con queste due tabelle:

    Tabella-news: id_news, testo_news, data_news

    Tabella-commenti: id_commento, id_news, testo_commento, data_commento

    Ogni news (tabella news) può essere commentata dagli utenti (tabella commenti). Dunque una news può avere infiniti commenti (relazione uno a molti).

    Io vorrei ordinare le news in base all'ultimo commento inserito; dunque in base alla data del commento. Questo mi porterebbe ad avere, ordinando in modo decrescente, un sistema che mi tiene aggiornato i movimenti in ogni news (stile forum dove le nuove risposte fanno risalire i messaggi).

    Come posso scrivere questa query?

    select * from Tabella-news order by ????

    GRAZIE

  2. #2
    prova a fare cosi:
    Tabella-news: id_news, testo_news, data_news

    Tabella-commenti: id_commento, id_news, testo_commento, data_commento
    Codice PHP:
    SELECT FROM Tabella-news AS 
    INNER JOIN Tabella
    -commenti AS c ON c.id_news n.id_news
    WHERE 1 ORDER BY c
    .data_commento DESC 
    MiServe di Santino Bivacqua
    PHP/Ruby/Node.js Developer
    www.miserve.com

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    541
    Originariamente inviato da sanbiv
    prova a fare cosi:
    Tabella-news: id_news, testo_news, data_news

    Tabella-commenti: id_commento, id_news, testo_commento, data_commento
    Codice PHP:
    SELECT FROM Tabella-news AS 
    INNER JOIN Tabella
    -commenti AS c ON c.id_news n.id_news
    WHERE 1 ORDER BY c
    .data_commento DESC 

    non funziona

  4. #4
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    prova così se puoi fare subquery (mysql almeno 4.1)
    codice:
    select * from(
    select max(id_commento) as commento 
    from commenti group by id_news
    order by data_commento desc
    ) 
    as tab1
    left join commenti as tab2 on tab1.commento = tab2.id_commento

  5. #5
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    892
    Non c'è bisogno di complicare tanto. Basta questo:

    codice:
    SELECT tabella-news.testo_news, tabella-news.data_news, tabella-commenti.testo_commento, tabella-commenti.data_commento FROM tabella-news INNER JOIN tabella-commento ON tabella-news.id_news=tabella-commenti.id_news ORDER BY id_commento DESC
    Se non va l'errore è nel codice...

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    541
    Originariamente inviato da nicola75ss
    prova così se puoi fare subquery (mysql almeno 4.1)
    codice:
    select * from(
    select max(id_commento) as commento 
    from commenti group by id_news
    order by data_commento desc
    ) 
    as tab1
    left join commenti as tab2 on tab1.commento = tab2.id_commento
    così funziona ma vorrei anche visualizzare le news che non hanno un commento. come fare

  7. #7
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    prova questa
    codice:
    select news.testo_news,tab2.id_commento,tab2.id_news,tab2.testo_commento,tab2.data_commento FROM(
    select max(id_commento) as commento 
    from commenti group by id_news
    order by data_commento desc
    ) 
    as tab1
    left join commenti as tab2 on tab1.commento = tab2.id_commento
    inner join news on news.id_news = tab2.id_news 
    union 
    select news.testo_news,commenti.id_commento,commenti.id_news,commenti.testo_commento,commenti.data_commento from news
    left join commenti on news.id_news = commenti.id_news
    where commenti.id_news is null

  8. #8
    Originariamente inviato da sanbiv
    prova a fare cosi:
    Tabella-news: id_news, testo_news, data_news

    Tabella-commenti: id_commento, id_news, testo_commento, data_commento
    Codice PHP:
    SELECT FROM Tabella-news AS 
    INNER JOIN Tabella
    -commenti AS c ON c.id_news n.id_news
    WHERE 1 ORDER BY c
    .data_commento DESC 
    Non capisco perche' non va la mia....
    comunque per visualizzare anche le news senza commenti usa right join a posto di left join...
    ciao e fammi sapere che problema ti dava la mia query....
    MiServe di Santino Bivacqua
    PHP/Ruby/Node.js Developer
    www.miserve.com

  9. #9
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    892
    così:
    codice:
    SELECT tabella-news.testo_news, tabella-news.data_news, tabella-commenti.testo_commento, tabella-commenti.data_commento FROM tabella-news LEFT JOIN tabella-commento ON tabella-news.id_news=tabella-commenti.id_news ORDER BY id_commento DESC
    insisto: non necessiti di subquery...

  10. #10
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    541
    Originariamente inviato da sanbiv
    Non capisco perche' non va la mia....
    comunque per visualizzare anche le news senza commenti usa right join a posto di left join...
    ciao e fammi sapere che problema ti dava la mia query....
    grazie ho risolto facendo qualche piccola modifica (right join, ecc...) sul codice di nicola75ss.

    @sanbiv: la tua mi dava dei problemi a livello di visualizzazione delle news.

    GRAZIE A TUTTI

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.