Jump to content

Query returns only one result, more entries exists


Recommended Posts

Hi,

I'm having some trouble with a query that is supposed to return all 'projects' connected to a specific 'customer', but only one is returned.

		SELECT
		projects.name AS name,
		projects.id AS id,
		SUM(reports.amount * reports.price) AS total,
		COUNT(DISTINCT reports.user) AS users
	FROM projects
		LEFT JOIN reports ON reports.project = projects.id
	WHERE projects.customer = :id
		ORDER BY projects.name ASC

 

There are several projects on each customer but this query only returns one of them. Also, the returning result does not seem to be affected by the ORDER BY clause as a project called "Port" is returned even though I have other projects that is supposed to be indexed before it (e.g "General").

 

What am I doing wrong?

// Thanks, Daniel

Well, ':id' is a defined variable that can be used with the PDO library using PHP:

<?php
	$projects = $this->pdo->prepare("
	SELECT
		projects.name AS name,
		projects.id AS id,
		SUM(reports.amount * reports.price) AS total,
		COUNT(reports.user) AS users
	FROM projects
		LEFT JOIN reports ON reports.project = projects.id
	WHERE projects.customer = :id
		GROUP BY reports.user
		ORDER BY projects.name ASC
	");
	$projects->bindValue(':id', $id, PDO::PARAM_STR);
	$projects->execute();
?>

 

The query returns a project and the projects are allways connected to the customer I'm browsing, so that should not be the problem.

 

Edit: $id contains the customer id.

When you execute the query directly against your database, using your favorite database management tool, does it produce the expected results?

 

I would guess that your presentation logic that is retrieving the results from the result set is probably at fault.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.