query with COUNT (what am I doing wrong?)

This is a discussion on query with COUNT (what am I doing wrong?) within the MySQL Database forums, part of the Database Forums category; Hi, I'm trying to run this query: --->SELECT COUNT(v.id) as C, v.title, v.desc, v....


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-25-2007
panchettone
 
Posts: n/a
Default query with COUNT (what am I doing wrong?)


Hi, I'm trying to run this query:

--->SELECT COUNT(v.id) as C, v.title, v.desc, v.cat, u.username, t.team_name
FROM videos AS v
INNER JOIN users AS u ON v.user_id = u.user_id
INNER JOIN team_members tm ON tm.user_id = u.user_id
INNER JOIN teams AS t ON tm.team_id = t.team_id

but I get an error
[
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.title, v.desc' at line 1
]

If I omit COUNT (but I need it...), it perfectly works.
What am I doing wrong? How could I rewrite my query? Thanks!
Reply With Quote
  #2 (permalink)  
Old 10-25-2007
Captain Paralytic
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

On 25 Oct, 12:00, panchettone <pan_che_ttone@non_morde.bau> wrote:
> Hi, I'm trying to run this query:
>
> --->SELECT COUNT(v.id) as C, v.title, v.desc, v.cat, u.username, t.team_name
> FROM videos AS v
> INNER JOIN users AS u ON v.user_id = u.user_id
> INNER JOIN team_members tm ON tm.user_id = u.user_id
> INNER JOIN teams AS t ON tm.team_id = t.team_id
>
> but I get an error
> [
> #1064 - You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near '.title, v.desc' at line 1
> ]
>
> If I omit COUNT (but I need it...), it perfectly works.
> What am I doing wrong? How could I rewrite my query? Thanks!


Try:

SELECT COUNT(v.id) cnt, v.title, v.desc, v.cat, u.username,
t.team_name
FROM videos AS v
INNER JOIN users AS u ON v.user_id = u.user_id
INNER JOIN team_members tm ON tm.user_id = u.user_id
INNER JOIN teams AS t ON tm.team_id = t.team_id

Reply With Quote
  #3 (permalink)  
Old 10-25-2007
panchettone
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)


>
> SELECT COUNT(v.id) cnt, v.title, v.desc, v.cat, u.username,
> t.team_name
> FROM videos AS v
> INNER JOIN users AS u ON v.user_id = u.user_id
> INNER JOIN team_members tm ON tm.user_id = u.user_id
> INNER JOIN teams AS t ON tm.team_id = t.team_id
>





a new error:
[
#1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP
columns is illegal if there is no GROUP BY clause
]
Reply With Quote
  #4 (permalink)  
Old 10-25-2007
Captain Paralytic
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

On 25 Oct, 12:27, panchettone <pan_che_ttone@non_morde.bau> wrote:
> > SELECT COUNT(v.id) cnt, v.title, v.desc, v.cat, u.username,
> > t.team_name
> > FROM videos AS v
> > INNER JOIN users AS u ON v.user_id = u.user_id
> > INNER JOIN team_members tm ON tm.user_id = u.user_id
> > INNER JOIN teams AS t ON tm.team_id = t.team_id

>
> a new error:
> [
> #1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP
> columns is illegal if there is no GROUP BY clause
> ]


Well that's basic GROUP BY syntax!

SELECT COUNT(v.id) cnt, v.title, v.desc, v.cat, u.username,
t.team_name
FROM videos AS v
INNER JOIN users AS u ON v.user_id = u.user_id
INNER JOIN team_members tm ON tm.user_id = u.user_id
INNER JOIN teams AS t ON tm.team_id = t.team_id
GROUP BY v.title, v.desc, v.cat, u.username, t.team_name

Reply With Quote
  #5 (permalink)  
Old 10-25-2007
panchettone
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

Captain Paralytic ha scritto:

>
> Well that's basic GROUP BY syntax!




well, the fact is I don't need any group by
Reply With Quote
  #6 (permalink)  
Old 10-25-2007
Captain Paralytic
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

On 25 Oct, 14:12, panchettone <pan_che_ttone@non_morde.bau> wrote:
> Captain Paralytic ha scritto:
>
>
>
> > Well that's basic GROUP BY syntax!

>
> well, the fact is I don't need any group by


OH, well you had better argue that out with MySQL because it says you
do!

If you are not happy with the syntax supplied by the MySQL product and
SQL, you had better write your own RDBMS and your own SQL standard!

Reply With Quote
  #7 (permalink)  
Old 10-25-2007
Jerry Stuckle
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

Captain Paralytic wrote:
> On 25 Oct, 14:12, panchettone <pan_che_ttone@non_morde.bau> wrote:
>> Captain Paralytic ha scritto:
>>
>>
>>
>>> Well that's basic GROUP BY syntax!

>> well, the fact is I don't need any group by

>
> OH, well you had better argue that out with MySQL because it says you
> do!
>
> If you are not happy with the syntax supplied by the MySQL product and
> SQL, you had better write your own RDBMS and your own SQL standard!
>
>


It's more than just MySQL, Paul. That's part of the SQL standard. In
fact, MySQL has historically been much more lenient than other databases.

IIRC, the SQL standard requires that when you mix aggregate functions
with columns (or non-aggregate functions) you must GROUP BY all
non-aggregate columns or functions. MySQL hasn't enforced it that much
in the past.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #8 (permalink)  
Old 10-25-2007
Captain Paralytic
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)

On 25 Oct, 14:41, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Captain Paralytic wrote:
> > On 25 Oct, 14:12, panchettone <pan_che_ttone@non_morde.bau> wrote:
> >> Captain Paralytic ha scritto:

>
> >>> Well that's basic GROUP BY syntax!
> >> well, the fact is I don't need any group by

>
> > OH, well you had better argue that out with MySQL because it says you
> > do!

>
> > If you are not happy with the syntax supplied by the MySQL product and
> > SQL, you had better write your own RDBMS and your own SQL standard!

>
> It's more than just MySQL, Paul. That's part of the SQL standard.


Errm, hence why I said "you had better write your own RDBMS and your
own SQL standard!"

Reply With Quote
  #9 (permalink)  
Old 10-25-2007
panchettone
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)


>
> OH, well you had better argue that out with MySQL because it says you
> do!
>
> If you are not happy with the syntax supplied by the MySQL product and
> SQL, you had better write your own RDBMS and your own SQL standard!
>




ok, I didn't want to start a fight, sorry for saying what I said, it was
my ignorance speaking for me. I was just wondering -since my query is
just working- if that group-by is something that can alter the way the
results are sorted by (the entire query I did not post ends with an
order by desc)
Reply With Quote
  #10 (permalink)  
Old 10-25-2007
panchettone
 
Posts: n/a
Default Re: query with COUNT (what am I doing wrong?)



> IIRC, the SQL standard requires that when you mix aggregate functions
> with columns (or non-aggregate functions) you must GROUP BY all
> non-aggregate columns or functions.




could you suggest a place online where I can find tips like this one?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:25 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0