This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 在mysql数据库里, 怎样用一条select同时选出最贵和最便宜的两条记录
-jaguarx(常驻rolia);
2002-10-13
(#796021@0)
-
You select max of something then union a select of min of something.
-jeffrey815(Smartiecat);
2002-10-13
(#796061@0)
-
mysql doesn't support union
-easyway(翠花,回家吧!);
2002-10-13
(#796075@0)
-
怎么样, 这个题有点难度吧.
-jaguarx(常驻rolia);
2002-10-14
(#796812@0)
-
这不算难,在国内开发报表软件曾用一条select 语句求出整个一张报表,每行的条件都不一样。现在没机会做这样的软件罗。
-easyway(翠花,回家吧!);
2002-10-14
(#796947@0)
-
just use sql like :select min(price),max(price) from yourtable.
-easyway(翠花,回家吧!);
2002-10-13
(#796072@0)
-
DX,敬业一点好不好,他要的是记录,不是值啊
-hugefox(长得象熊的狐狸);
2002-10-13
(#796467@0)
-
那就:select * from Table_name where price = (select max(price) from Table_name) or price = (select min(price) from Table_name) order by price 不过mysql下行不行,我不知道。
-guestagain(guest again);
2002-10-13
(#796490@0)
-
select max(price) from Table_name是不是该改为select max(price) from price?很久没有用mysql,不是特别确定
-heian(黑暗®-出售CF GPS);
2002-10-14
(#796514@0)
-
你要好好看看sql了。
-guestagain(guest again);
2002-10-14
(#796522@0)
-
?不是从column里面找吗?一年多没用了,忘的差不多了。
-heian(黑暗®-出售CF GPS);
2002-10-14
(#796530@0)
-
hehe
-guestagain(guest again);
2002-10-14
(#796534@0)
-
别光hehe,讲解讲解啊,趁我现在还没忘光。
-heian(黑暗®-出售CF GPS);
2002-10-14
(#796542@0)
-
max(price) already specify the column name.
-jeffrey815(Smartiecat);
2002-10-14
(#796553@0)
-
对对,是我想错了,后面是Table_name。
-heian(黑暗®-出售CF GPS);
2002-10-14
(#796562@0)
-
mysql doesn't support embeded select either.
solution :mysql> select b.* from test1 a,test1 b group by b.cost having b.cost = max(a.co
st) or b.cost = min(a.cost) ;
-easyway(翠花,回家吧!);
2002-10-14
(#796559@0)
-
you can only group by b.group when you select b.* ?
-guestagain(guest again);
2002-10-14
(#796565@0)
-
I just run it on my computer,no problem.
-easyway(翠花,回家吧!);
2002-10-14
(#796575@0)
-
是可以,不过好像和标准的sql有点不一样。
-guestagain(guest again);
2002-10-14
(#796590@0)
-
我明白你的意思,不过其他数据库也有方法实现。
-easyway(翠花,回家吧!);
2002-10-14
(#796608@0)
-
我知道呀。mysql还是挺快的,如果其他主流数据库也可以这样group法,那得省多少事呀。
-guestagain(guest again);
2002-10-14
(#796617@0)
-
行啊. 够专业. 能解释一下吗?
-jaguarx(常驻rolia);
2002-10-14
(#796811@0)
-
好吧,解释在正文里,打这几个字消耗了我两支烟,哈哈。为什么要用group by 如果不用group by 在having子句就不能使用b.cost,其实group by任何字段都行。那聚合运算用在条件子句一般要求使用having子句。
-easyway(翠花,回家吧!);
2002-10-14
{137}
(#796940@0)
-
不过还是有点问题,如果最大或者最小的记录不止一条,上面的sql只能找出一条,有什么办法全找出来吗?
-guestagain(guest again);
2002-10-14
(#796993@0)
-
"最大或着最小的记录不止一条",你的意思是说有一条以上相同的最大/小价格?
-greatchina(2002);
2002-10-14
(#796995@0)
-
是。哎!话都说不清楚了。
-guestagain(guest again);
2002-10-14
(#796999@0)
-
哈哈,你真会穷追猛打,group by id 就行了。
-easyway(翠花,回家吧!);
2002-10-14
(#797009@0)
-
多谢,主要是第一次见到group by里可以不包括所有select里有的field,有点乱,但为什么其他多数数据库都有这个限制呢?
-guestagain(guest again);
2002-10-14
(#797168@0)
-
从语法严谨角度应该group by all fields except the fields you do computation,MYSQL就没这个限制。mysql is mysql,haha.
-easyway(翠花,回家吧!);
2002-10-14
(#797169@0)