By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,136 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,136 IT Pros & Developers. It's quick & easy.

SELECT problem using GROUP BY and LIMIT

P: n/a

Folks,

I have a SELECT that returns with multiple records - It works when I have a
LIMIT clause but when I try to include a GROUP BY clause, the select returns
nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the
only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with
PHP - they are not created by MySQL) I've tried to simplify my query below,
keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
Jul 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
BDR


Randell D. wrote:
Folks,

I have a SELECT that returns with multiple records - It works when I have a
LIMIT clause but when I try to include a GROUP BY clause, the select returns
nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the
only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with
PHP - they are not created by MySQL) I've tried to simplify my query below,
keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


Does it work using SELECT DISTINCT myHash ? (without the group by)

Jul 19 '05 #2

P: n/a
BDR


Randell D. wrote:
Folks,

I have a SELECT that returns with multiple records - It works when I have a
LIMIT clause but when I try to include a GROUP BY clause, the select returns
nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the
only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with
PHP - they are not created by MySQL) I've tried to simplify my query below,
keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


Does it work using SELECT DISTINCT myHash ? (without the group by)

Jul 19 '05 #3

P: n/a
Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.

"Randell D." <yo**************************@yahoo.com> wrote in message news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a
LIMIT clause but when I try to include a GROUP BY clause, the select returns
nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the
only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with
PHP - they are not created by MySQL) I've tried to simplify my query below,
keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx

Jul 19 '05 #4

P: n/a
Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.

"Randell D." <yo**************************@yahoo.com> wrote in message news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a
LIMIT clause but when I try to include a GROUP BY clause, the select returns
nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the
only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with
PHP - they are not created by MySQL) I've tried to simplify my query below,
keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx

Jul 19 '05 #5

P: n/a
> "Randell D." <yo**************************@yahoo.com> wrote in message
news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx

"Rob Baxter" <ro*@microjuris.com> wrote in message
news:ac**************************@posting.google.c om... Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.


The myHash is unique in another table that matches the hash to a user. The
table I am working on relates to a uploads performed by the user (I'm not
storing the file data, just the file name). Each user can have more than
one entry from each user (thus more than one record with the same hash)
because each user can (but does not have to) upload one or more files.

I want to grab a unique list of the hashes - If a user has more than one
record (thus more than one file uploaded) I don't really care if its the
first or last hash just hand me one of each hash...

I believed I could do this by doing a GROUP BY on the hash... Am I wrong?
I've only been doing MySQL for six months or so...

and... thanks for the help...
Jul 19 '05 #6

P: n/a
> "Randell D." <yo**************************@yahoo.com> wrote in message
news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx

"Rob Baxter" <ro*@microjuris.com> wrote in message
news:ac**************************@posting.google.c om... Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.


The myHash is unique in another table that matches the hash to a user. The
table I am working on relates to a uploads performed by the user (I'm not
storing the file data, just the file name). Each user can have more than
one entry from each user (thus more than one record with the same hash)
because each user can (but does not have to) upload one or more files.

I want to grab a unique list of the hashes - If a user has more than one
record (thus more than one file uploaded) I don't really care if its the
first or last hash just hand me one of each hash...

I believed I could do this by doing a GROUP BY on the hash... Am I wrong?
I've only been doing MySQL for six months or so...

and... thanks for the help...
Jul 19 '05 #7

P: n/a

"BDR" <jo*@noemail.com> wrote in message
news:KP******************@bignews5.bellsouth.net.. .


Randell D. wrote:
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


Does it work using SELECT DISTINCT myHash ? (without the group by)


I had not heard of DISTINCT before (I'm six months a newbie on MySQL) - I
think I'll use it... except... I think I found my problem - My select query
was too long... I was performing my tests on the command line with only two
hashes, but once I copied pasted and did the test with 12hashes I got an
error (and the command line client exited out to my bash shell which took me
by surprise).

anyway... thanks for the suggestion...
Jul 19 '05 #8

P: n/a

"BDR" <jo*@noemail.com> wrote in message
news:KP******************@bignews5.bellsouth.net.. .


Randell D. wrote:
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


Does it work using SELECT DISTINCT myHash ? (without the group by)


I had not heard of DISTINCT before (I'm six months a newbie on MySQL) - I
think I'll use it... except... I think I found my problem - My select query
was too long... I was performing my tests on the command line with only two
hashes, but once I copied pasted and did the test with 12hashes I got an
error (and the command line client exited out to my bash shell which took me
by surprise).

anyway... thanks for the suggestion...
Jul 19 '05 #9

P: n/a

"Rob Baxter" <ro*@microjuris.com> wrote in message
news:ac**************************@posting.google.c om...
Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.

"Randell D." <yo**************************@yahoo.com> wrote in message

news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


I think I found the source of my problem was related to having a select
query that was too long - My initial tests were only on a couple of hashes,
but once I put in 12 of 'em, on the command line, things ended in error...
I'll have to reapproach my problem from a different angle, perhaps putting
in another column that can identify a common denominator with all the
hashes - Its not that difficult for me and will probably be alot more
performance friendly too..

thanks for the help though.
Jul 19 '05 #10

P: n/a

"Rob Baxter" <ro*@microjuris.com> wrote in message
news:ac**************************@posting.google.c om...
Randell,

First off you have an error in you SQL. The GROUP BY must come before
the LIMIT.

Secondly, What are you trying to accomplish with your GROUP BY
statement? Even if you correct the order of the GROUP BY and LIMIT
clauses it doesn't really accomplish anything. Essentially it is
functioning like an order by because you are not doing any aggregate
operations like COUNT or SUM. Tell us what you are trying to
accomplish.

"Randell D." <yo**************************@yahoo.com> wrote in message

news:<h_Ljb.112089$pl3.54067@pd7tw3no>...
Folks,

I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors).

The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db).

Where am I going wrong? Because my select uses hashes (which I provide with PHP - they are not created by MySQL) I've tried to simplify my query below, keeping the statements the same but my values abbrieviated.

** The following works: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10;

** The following fails: **
SELECT myHash FROM myTable
WHERE myHash='abc' OR myHash='def'
OR myHash='ghi' LIMIT 10
GROUP BY myHash;

Can anybody suggest where I'm going wrong?

Thanx


I think I found the source of my problem was related to having a select
query that was too long - My initial tests were only on a couple of hashes,
but once I put in 12 of 'em, on the command line, things ended in error...
I'll have to reapproach my problem from a different angle, perhaps putting
in another column that can identify a common denominator with all the
hashes - Its not that difficult for me and will probably be alot more
performance friendly too..

thanks for the help though.
Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.