select count(*) as sum ,tm.name from user as tm guoup by name 我 这个 语句 有问题, 我是 想实现 执行 sql select语句后 实现 如下结果:
sum name
1 jim
2 tom
3 robert
4 maly
5 kim 在 mySQL中 写 sql 语句 有没有 办法 实现 这种 功能
高手 指教 ,,,谢谢。
sum name
1 jim
2 tom
3 robert
4 maly
5 kim 在 mySQL中 写 sql 语句 有没有 办法 实现 这种 功能
高手 指教 ,,,谢谢。
解决方案 »
- load data local infile "d:\\中文.txt" into table p4 提示找不到数据,非中文就可以
- Mysql 我这样的多表查询如何提高速度 ?
- 一个表里2百个字段,会不会很一向性能
- 着急!!!在postgres中插入字符串中含有“‘”单引号怎么处理
- 把mysql的data目录下的数据库文件拷贝到另一台机器的mysql的data目录下,为什么myisam的表可以用,innoDB却出错?
- 那里可以下载mysql的源代码for freebsd版本的?
- 各位大哥,小弟求条sql,题目如下
- 如何配置mm.mysql.jdbc即JDBC关于MYSql的支持,解决者送50分。!!!求你了!
- 求助:MYSQL的odbc配置
- 请各位大侠,帮忙看一下下面触发器Before Insert 语句是否会引起死锁问题?
- 帮我改一下这个MYSQL的SQL语句(CASE WHEN```)
- 问一个文章管理系统数据库设计的问题
orselect name,count(*) as sum , from user group by name order by sum
在我的 数据库表中 name 是 主键我这个 按你的语句 执行后 sum 显示的 都是 1
name
jim
tom
robert
maly
kim
是记录?排序依据?
是数据库表中的记录。。 jim
tom
robert
maly
kim 这些是 name 字段的 一个 值
from user as tm
guoup by name
order by 1[align=center]==== 思想重于技巧 ====
[/align]
corresponds to your MySQL server version for the right syntax to use near 'guoup
by name
order by 1' at line 3
是的数据库里
------------------
有 2条记录的话
sum name
1 jim
2 tom
------------------
有 5条记录的话
sum name
1 jim
2 tom
3 robert
4 maly
5 kim
-----------------按这种 方式 显示,,是否 可行
from user as tm
guoup by `name`
order by 1 [align=center]==== 思想重于技巧 ====
[/align]
[/align]
select identity(int,1,1) as sum,name into newtt from tt
from user a , user b
where a.name > b.name
group by 1[align=center]==== 思想重于技巧 ====
[/align]
select count(*) as sno,a.name
from user a , user b
where a.name >= b.name
group by 1是>= [align=center]==== 思想重于技巧 ====
[/align]
如下提示 错误
ERROR 1056 (42000): Can't group on 'sno'
ERROR 1056 (42000): Can't group on 'sno'
mysql> select identity(int,1,1) as sum,name into newtt from userno;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'int,1
,1) as sum,name into newtt from user' at line 1
临时表查询。
select AUTO_INCREMENT(int,1,1) as sum,name into newtt from userERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '(int,
1,1) as sum,name into newtt from userno' at line 1还是不行,,,谢谢你 帮了我以上午 了我自己 再 想想 办法吧
执行INSERT 临时表 SELECT * FROM 工作表,再对
临时表查询。
INSERT into 临时表(f1,f2,f3) SELECT f1,f2,f3 FROM 工作表
FROM `phome_ecms_pub_news`
GROUP BY classid
ORDER BY mm结果:mm classid
1 48
1 53
1 57
2 49
2 51
2 55
3 52
4 50
6 38
7 44
9 43
10 47
13 56
13 58
19 42
37 45
41 59
42 46
146 54
233 37
237 39
301 40
303 41
概念没搞清啊,我可没法写,呵呵.即按记录写入表时的顺序,就是记录的物理位置,用过VFP的知道,有一个取记录号的函数RECNO()。
假如表中存放是这样的:
id name other
0 jim ……
1 kim ……
2 soso ……
3 jim ……
4 soso ……
5 soso ……
是不是查询后结果是这样:sum name
2 jim
1 kim
3 soso
汗!!!!!
来源: ChinaUnix博客 日期: 2008.07.04 12:48 (共有0条评论) 我要评论
如果需要在查询语句返回的列中包含一列表示该条记录在整个结果集中的行号,
ISOSQL:2003
标准提出的方法是
提供 ROW_NUMBER() / RANK() 函数
。 Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的
ROWNUM
; MS SQL Server 则在 2005 版本中提供了
ROW_NUMBER()
函数;但在 MySQL 中似乎还没有这样的系统自带功能。虽然 LIMIT 可以很方便的对返回的结果集数量和位置进行过滤,但过滤出来的记录的行号却没办法被 SELECT 到。
据说
MySQL 是早就想增加这个功能了,但我是还没找到。
解决方法是通过
预定义用户变量
来实现:
set @mycnt = 0;
select (@mycnt := @mycnt + 1) as ROWNUM , othercol from tblname order by othercol;
这样查询出来的结果集中 ROWNUM 就保存了行编号信息。这个行编号信息的某种用途在于当你需要根据需要对数据按照某种规则排序并取出排序之后的某一行数据,并且希望知道这行数据在之前排序中的位置时就用得着了。比如:
set @mycnt = 0;
select * from (
select (@mycnt := @mycnt + 1) as ROWNUM , othercol
from tblname order by othercol
) as A where othercol=OneKeyID;
当然你也可以
通过创建临时表的方法
把
查询结果写到某个拥有 auto_increment 字段的临时表中再做查询,但考虑到临时表在 MySQL master / slave
模式下可能产生的问题,用这样临时用户定义变量的方式来计算查询结果集每一行对应的行号还是更为简洁 -- 除非你愿意在 PHP
或其他语言脚本中对返回的整个结果集再作处理。