Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [MySQL] Query non funzionante

    Ciao a tutti.

    Ho creato un database per un news system, ecco il dump della struttura

    codice:
    CREATE TABLE category (
      id tinyint(4) NOT NULL AUTO_INCREMENT,
      `name` varchar(30) NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
    
    CREATE TABLE `comment` (
      id mediumint(9) NOT NULL AUTO_INCREMENT,
      author varchar(30) NOT NULL,
      authorEmail varchar(100) NOT NULL,
      authorURL varchar(200) NOT NULL,
      authorIP varchar(100) NOT NULL,
      `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      isApproved tinyint(1) NOT NULL DEFAULT '0',
      post_id smallint(6) NOT NULL,
      content text NOT NULL,
      karma mediumint(9) NOT NULL DEFAULT '0',
      PRIMARY KEY (id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
    
    CREATE TABLE post (
      id smallint(6) NOT NULL AUTO_INCREMENT,
      title varchar(50) NOT NULL,
      category_id tinyint(4) NOT NULL,
      author_id tinyint(4) NOT NULL DEFAULT '0',
      content longtext NOT NULL,
      isPublished tinyint(1) NOT NULL DEFAULT '0',
      creationDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      lastEditDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      likes smallint(6) NOT NULL,
      hates smallint(6) NOT NULL,
      PRIMARY KEY (id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
    
    CREATE TABLE `user` (
      id tinyint(4) NOT NULL AUTO_INCREMENT,
      `name` varchar(16) NOT NULL,
      pass varchar(100) NOT NULL,
      IPaddress varchar(12) NOT NULL DEFAULT '0',
      permission_id tinyint(1) NOT NULL,
      PRIMARY KEY (id),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
    Le tabelle sono queste 4:
    [list=1][*]post - gli articoli[*]user - gli utenti che scrivono gli articoli[*]comment - i commenti agli articoli[*]category - le categorie degli articoli[/list=1]

    Il problema è che non riesco a fare una semplice query che dovrebbe restituire questi campi:

    codice:
    post.title, user.name, post.creationDate, category.name, post.content, COUNT(comment.id), post.likes, post.hates

    Questo è il mio tentativo. Ho tralasciato category.name per dimenticanza, ma non fa niente per ora... intanto vorrei capire dove sbaglio con questa query

    codice:
    SELECT a.title, u.name AS author, a.creationDate, a.content, COUNT( c.id ) AS comments, a.likes, a.hates
    FROM (
    backend.post AS a
    INNER JOIN backend.user AS u ON a.author_id = u.id
    )
    INNER JOIN backend.comment AS c ON c.post_id = a.id
    ORDER BY creationDate DESC
    Praticamente mi restituisce solo un risultato, il primo articolo.
    Come posso risolvere?

    Ringrazio in anticipo

  2. #2
    Ho risolto, mancava il GROUP BY a.id

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.