使用GROUP BY和LIMIT选择问题 [英] SELECT problem using GROUP BY and LIMIT

查看:1172
本文介绍了使用GROUP BY和LIMIT选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



伙计,


我有一个带有多条记录的SELECT - 当我有一个

LIMIT时它会工作但是当我尝试包含GROUP BY子句时,select返回

什么都没有(即没有记录,没有错误)。


GROUP BY指定一个我的数据库中确实存在的列(也是从数据库中选择的

唯一字段)。


我哪里错了?因为我的选择使用哈希(我提供的是

PHP - 它们不是由MySQL创建的)我试图在下面简化我的查询,

保持语句同样但我的价值观有所不同。


**以下作品:**

SELECT myHash from myTable

WHERE myHash =' 'abc''或myHash =''def''

或myHash =''ghi''限制10;


**以下失败:* *

选择myHash来自myTable

WHERE myHash =''abc''或myHash =''def''

或myHash ='' ghi''限制10

GROUP by myHash;


有谁可以建议我哪里出错?


Thanx

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么是top-发布这么糟糕的事情?

A:热门发布。

问:usenet上最烦人的事情是什么?

解决方案

< blockquote>


Randell D.写道:

人们,

我有一个返回多条记录的SELECT - 它工作时我有一个
LIMIT子句但是当我尝试包含GROUP BY子句时,select返回
什么都没有(即没有记录,没有错误)。

GROUP BY指定一个我的数据库中确实存在的列(也是从数据库中选择的唯一字段)。

我哪里错了?因为我的选择使用哈希(我用PHP提供它们 - 它们不是由MySQL创建的)我试图简化下面的查询,
保持语句相同,但我的值得到了消除。 />
**以下作品:**
SELECT myHash FROM myTable
WHERE myHash =''abc''或myHash =''def''
或myHash =' 'ghi''限制10;

**以下失败:**
SELECT myHash from myTable
WHERE myHash =''abc''或myHash =''def' '
或myHash =''ghi''限制10
GROUP by myHash;

任何人都可以建议我哪里出错?

Thanx




它是否可以使用SELECT DISTINCT myHash? (没有小组)





Randell D.写道:

伙计,

我有一个返回多个记录的SELECT - 当我有一个
LIMIT子句但当我尝试包含GROUP BY子句时,select会返回
什么都没有(即没有记录,没有错误)。

GROUP BY指定一个确实存在于我的数据库中的列(并且也是从数据库中选择的唯一字段)。 br />
我哪里错了?因为我的选择使用哈希(我用PHP提供它们 - 它们不是由MySQL创建的)我试图简化下面的查询,
保持语句相同,但我的值得到了消除。 />
**以下作品:**
SELECT myHash FROM myTable
WHERE myHash =''abc''或myHash =''def''
或myHash =' 'ghi''限制10;

**以下失败:**
SELECT myHash from myTable
WHERE myHash =''abc''或myHash =''def' '
或myHash =''ghi''限制10
GROUP by myHash;

任何人都可以建议我哪里出错?

Thanx




它是否可以使用SELECT DISTINCT myHash? (没有小组)


Randell,


首先你在SQL中有错误。 GROUP BY必须在LIMIT之前获得
.


其次,你想用GROUP BY

声明完成什么? ?即使你纠正了GROUP BY和LIMIT

条款的顺序,它也没有真正完成任何事情。基本上它是

的功能就像一个订单,因为你没有做任何聚合

操作,如COUNT或SUM。告诉我们你要做什么

完成。


" Randell D." <哟************************** @ yahoo.com>在消息新闻中写道:< h_Ljb.112089



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?

解决方案



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)




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)


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


这篇关于使用GROUP BY和LIMIT选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆