// Include the package library use Doctrine\DBAL\Query\QueryBuilder; // Create a new query builder object $qb = new QueryBuilder($conn); // Build the query $qb->select('username') ->distinct() ->from('users'); // Execute the query and fetch results $results = $qb->execute()->fetchAll();
// Include the package library use Doctrine\DBAL\Query\QueryBuilder; // Create a new query builder object $qb = new QueryBuilder($conn); // Build the query $qb->addSelect('COUNT(*) as total') ->addSelect('username') ->from('users') ->groupBy('username') ->orderBy('total', 'DESC') ->setMaxResults(10); // Use setDistinct method to remove duplicates $qb->setDistinct(true); // Execute the query and fetch results $results = $qb->execute()->fetchAll();In this example, we are using setDistinct() method to fetch the top 10 users with the most posts. The query counts the total number of entries per user and groups the results by the username. With the use of setDistinct() method, we are removing any duplicate entries in the result set. Package Library: Doctrine\DBAL\Query\QueryBuilder