473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SELECT problem using GROUP BY and LIMIT


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
10 14018
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
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
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
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
> "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
> "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

"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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Irlan agous | last post by:
Hello i have t tables reactie and form and this query $sql = "select reactie.persid,form.oproep,form.foto,form.id from reactie,form INNER JOIN reactie ON (reactie.persid = form.id) group by...
0
by: rg | last post by:
Hello, I use invisionboard on a bid web site (20 000 visitors each day) But my server has high load average. Could you help me to optimize these select command. 1) SELECT p.*,...
16
by: Andie | last post by:
Hello All, I have this sql statement thats works in MSSQL when I call a function function showNewProd(dispNum) 'Declare some variables dim mySQL, rsTemp, tempStr, count 'Read Database
3
by: Jay K | last post by:
Hi, I have multiple queries like this: SELECT col1, col2, col3, col4 FROM table1, table2 where table1.col1 = table2.col1 and table1.col2 = 1 ORDER BY col3 desc LIMIT 5 and
3
by: Wim Roffil | last post by:
Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a...
12
by: Alex | last post by:
Hi, I have a column with a sequence in a table and want to allocate the first 200 for special purpose thus starting the sequence from 200. For the first 200 I have entries that will be added and...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
3
by: jon | last post by:
Please Excuse the newbie question, but I have not been able to figure out how to do the following on my own. Assume I have a table called 'reviews' and in that table there are three columns:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.