472,780 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 13976
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.