Probleme mit COUNT in einer Select abfrage

Das funktioniert,

$sql = "select id,titel,date from tabelle WHERE date BETWEEN
DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND '".date('Y-m-d')."' ";

$result = mysql_query($sql);
while ($ausgabe = mysql_fetch_object($result)) {


doch jetzt wo ich die Titel zähle bekomme ich einen Error und ich weis
wirklich nicht warum "Warning: mysql_fetch_object(): supplied argument is
not a valid MySQL result resource in"


$sql = "select id,titel,count( titel)AS anzahl, date from tabelle GROUP BY
titel WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
'".date('Y-m-d')."' ";

$result = mysql_query($sql);
while ($ausgabe = mysql_fetch_object($result)) {


Maria
Maria Sudderman [ So, 08 Juli 2007 11:12 ] [ ID #1762920 ]

Re: Probleme mit COUNT in einer Select abfrage

Maria Sudderman meinte:
> Das funktioniert,
>
> $sql = "select id,titel,date from tabelle WHERE date BETWEEN
> DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND '".date('Y-m-d')."' ";
>
> $result = mysql_query($sql);
> while ($ausgabe = mysql_fetch_object($result)) {
>
>
> doch jetzt wo ich die Titel zähle bekomme ich einen Error und ich weis
> wirklich nicht warum "Warning: mysql_fetch_object(): supplied argument is
> not a valid MySQL result resource in"
>
>
> $sql = "select id,titel,count( titel)AS anzahl, date from tabelle GROUP BY
> titel WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
> '".date('Y-m-d')."' ";
>
> $result = mysql_query($sql);
> while ($ausgabe = mysql_fetch_object($result)) {
>

Die WHERE-Klausel gehört *vor* die GROUP BY-Klausel. Oder du verwendest
HAVING. "Where" filtert vor der Gruppierung, "Having" nachher.

Gregor


--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Gregor Kofler [ So, 08 Juli 2007 11:21 ] [ ID #1762921 ]

Re: Probleme mit COUNT in einer Select abfrage

Maria Sudderman schrieb:

> $result =3D mysql_query($sql);

Mit einer minimalen Fehlerbehandlung ermöglichst du MySQL, dir zu sagen=
,
wo's hakt:

$result =3D mysql_query($sql) or die($sql.'<br>'.mysql_error());

MfG
Niels

--
| http://www.kolleg.de =B7 Das Portal der Kollegs in Deutschland |
| http://www.bsds.de =B7 BSDS Braczek Software- und DatenSysteme |
| Webdesign =B7 Webhosting =B7 e-Commerce =B7 Joomla! Content Management =
|
------------------------------------------------------------ ------
Niels Braczek [ So, 08 Juli 2007 15:00 ] [ ID #1762922 ]

Re: Probleme mit COUNT in einer Select abfrage

This is a multi-part message in MIME format.
--------------040104000901050803030904
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Maria Sudderman schrieb:
> $sql = "select id,titel,count( titel)AS anzahl, date from tabelle GROUP BY
> titel WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
> '".date('Y-m-d')."' ";
>
in o.g. Beispiel stimmen die Select Spalten auch nicht mit den
gewünschten Aliasen überein.
Natürlich hat Gregor auch recht.
Ich denke mal so sollte das statement funktionieren:

SELECT id,titel,COUNT(titel)
AS alias_id, alias_titel, alias_anzahl
FROM tabelle
WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
'".date('Y-m-d')."'
GROUP BY titel

so long, Ben ;:O)

--------------040104000901050803030904
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<small><font face="Tahoma">Maria Sudderman schrieb:</font></small>
<blockquote cite="mid:f6q9le$n6s$01$1 [at] news.t-online.com" type="cite">
<pre wrap="">
$sql = "select id,titel,count( titel)AS anzahl, date from tabelle GROUP BY
titel WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
'".date('Y-m-d')."' ";
</pre>
</blockquote>
<small><font face="Tahoma">in o.g. Beispiel stimmen die Select Spalten
auch nicht mit den gewünschten Aliasen überein.<br>
Natürlich hat Gregor auch recht.<br>
Ich denke mal so sollte das statement funktionieren:</font></small><br>
<br>
<font face="Courier New">SELECT id,titel,COUNT(titel)<br>
  AS alias_id, alias_titel, alias_anzahl  <br>
FROM tabelle <br>
WHERE date BETWEEN DATE_SUB('".date('Y-m-d')."',INTERVAL 1 month) AND
'".date('Y-m-d')."'<br>
GROUP BY titel <br>
<br>
<small><font face="Tahoma">so long, Ben ;:O)</font></small><br>
</font>
</body>
</html>

--------------040104000901050803030904--
Ben Dorato [ Mo, 09 Juli 2007 16:06 ] [ ID #1763635 ]

Re: Probleme mit COUNT in einer Select abfrage

Ben Dorato schrieb:

> Content-Type: text/html; charset=ISO-8859-1

Bitte kein HTML in den Newsgroups.

Gruß. Claus
Claus Reibenstein [ Mo, 09 Juli 2007 16:24 ] [ ID #1763636 ]
PHP » de.comp.lang.php.datenbanken » Probleme mit COUNT in einer Select abfrage

Vorheriges Thema: Was ist schneller?
Nächstes Thema: PHP 5 & MSSQL Enterprise Server