473,765 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14051
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.112 089$pl3.54067@p d7tw3no>...
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.112 089$pl3.54067@p d7tw3no>...
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.112 089$pl3.54067@p d7tw3no>...
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.goo gle.com... 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.112 089$pl3.54067@p d7tw3no>...
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.goo gle.com... 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.co m> wrote in message
news:KP******** **********@bign ews5.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.co m> wrote in message
news:KP******** **********@bign ews5.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.goo gle.com...
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.112 089$pl3.54067@p d7tw3no>...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2010
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 reactie.persid order by reactie.persid DESC LIMIT 0,5"; $result = mysql_query($sql);
0
1662
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.*, m.id,m.name,m.mgroup,m.email,m.joined,m.avatar,m.avatar_size,m.posts,m.aim_name,m.icq_number,
16
3191
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
2625
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
2400
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 different set of results. What is the best way to achieve this effect?
12
2350
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 removed. Is there an easy way to write a select statement that returns me the frist free number or any within the range of 200? For example if 1-30, and 32-50 are occupied then i would like to fill in the new entry with id 31. I currently do it...
3
6472
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 COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
6
13024
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 is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
3
5694
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: reviewDateTime, reviewID, and reviewItem. reviewItem is not unique, the other two are. What I want to do in pseudo-code is write a select query that will: for each unique reviewItem select 5 reviewID by reviewDateTime ascending
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.