PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   MySQLi/PDO/(MySQL) (http://www.selfphp.de/forum/forumdisplay.php?f=22)
-   -   Hilfe mit count bzw. sum (http://www.selfphp.de/forum/showthread.php?t=24290)

bonanza10 20.07.2011 09:35:45

Hilfe mit count bzw. sum
 
Ich hab eine Tabelle in der Kommentare gespeichert werden, zusätzliche eine Tabelle in der ich speichere ob jemand diesen Kommentar hilfreich findet (so wie bei Amazon)
Die Kommentrae rufe ich so ab:
Code:

SELECT pc.`id_product_comment` , IF( c.id_customer, CONCAT( c.`firstname` , ' ', LEFT( c.`lastname` , 1 ) ) , pc.customer_name ) customer_name, pc.`content` , pc.`grade` , pc.`date_add` , pc.title
FROM `ps_product_comment` pc
LEFT JOIN `ps_customer` c ON c.`id_customer` = pc.`id_customer`
WHERE pc.`id_product` =11
ORDER BY pc.`date_add` DESC
LIMIT 0 , 30

Das klappt so weit ganz gut, ich bekomme meine 3 Datensätze zurück. Nun will ich aber wissen wie viele User einen Kommentar hilfreich fanden, was so aussieht:
Code:

SELECT
pc.`id_product_comment`,
sum(if(hp.helpful=1,1,0)) AS helpful,
IF(c.id_customer, CONCAT(c.`firstname`, ' ', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name,
pc.`content`,
pc.`grade`,
pc.`date_add`,
pc.title
FROM `ps_product_comment` pc
LEFT JOIN `ps_customer` c ON c.`id_customer` = pc.`id_customer`
LEFT JOIN `wr_comment_helpful` hp ON ( pc.`id_product_comment` = hp.`id_product_comment`)
WHERE pc.`id_product` = 11
ORDER BY pc.`date_add` DESC

Leider stimmt da irgendwas nicht, ich bekomme lediglich noch einen Datensatz zurück.
Pseudo SQL sollte so sein:
Code:

SELECT
pc.`id_product_comment`,
ALLE DATENSÄTZE DIE ZUM KOMMENTAR GEHÖREN UND HILFREICH SIND AS helpful,  <--------
ALLE DATENSÄTZE DIE ZUM KOMMENTAR GEHÖREN AS all,                        <---------
IF(c.id_customer, CONCAT(c.`firstname`, ' ', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name,
pc.`content`,
pc.`grade`,
pc.`date_add`,
pc.title
FROM `ps_product_comment` pc
LEFT JOIN `ps_customer` c ON c.`id_customer` = pc.`id_customer`
LEFT JOIN `wr_comment_helpful` hp ON ( pc.`id_product_comment` = hp.`id_product_comment`)      <-------------------------
WHERE pc.`id_product` = 11
ORDER BY pc.`date_add` DESC

Kann mir jemand einen Tipp geben, ich komm da einfach nicht weiter.
Danke schon mal!

Matthias 20.07.2011 09:57:54

AW: Hilfe mit count bzw. sum
 
Du musst noch definieren nach was gruppiert werden soll. Schau dir mal group by an.

Deine Query sollte so das richtige Ergebnis liefern:

Code:

SELECT
pc.`id_product_comment`,
sum(if(hp.helpful=1,1,0)) AS helpful,
IF(c.id_customer, CONCAT(c.`firstname`, ' ', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name,
pc.`content`,
pc.`grade`,
pc.`date_add`,
pc.title
FROM `ps_product_comment` pc
LEFT JOIN `ps_customer` c ON c.`id_customer` = pc.`id_customer`
LEFT JOIN `wr_comment_helpful` hp ON ( pc.`id_product_comment` = hp.`id_product_comment`)
WHERE pc.`id_product` = 11
GROUP BY pc.`id_product_comment`
ORDER BY pc.`date_add` DESC


bonanza10 20.07.2011 10:12:04

AW: Hilfe mit count bzw. sum
 
Danke dir, das wars :-)


Alle Zeitangaben in WEZ +2. Es ist jetzt 12:43:09 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.