免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3822 | 回复: 13
打印 上一主题 下一主题

求语句 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-17 10:27 |只看该作者 |倒序浏览
系统fedora6 MYSQL5.0.22

radcheck 表 username  ipaddr              2字段
radacct 表 username CallingStationId   2字段

现将2表username相同的记录,从CallingStationId  传到ipaddr 中.

我mysql> update a set a.ipaddr=b.CallingStationId from radcheck a,radacct b where a.username=b.username;
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 'from radcheck a,radacct b where a.username=b.username' at line 1

mysql> update radcheck set radcheck.ipaddr=radacct.CallingStationId where radcheck.username=radacct.username ;
ERROR 1054 (42S22): Unknown column 'radacct.username' in 'where clause'
mysql>

那具体该怎么写??/

论坛徽章:
0
2 [报告]
发表于 2007-07-17 11:32 |只看该作者
测试用代码:

create table chk(
  user varchar(8) not null,
  ip   char(15)  not null
);

create table act(
  user  varchar(8) not null,
  sttid char(15)  not null
);

insert into act values
('tom','1.1.1.1'),
('James','1.1.1.2'),
('Peter','1.1.1.3'),
('Joe','1.1.1.3');
insert into chk values
('tom',''),
('James',''),
('Peter',''),
('Robot','');

update chk as x
set ip=(select sttid from act as y where x.user=y.user)
where x.user in (select user from act);

论坛徽章:
0
3 [报告]
发表于 2007-07-17 11:33 |只看该作者
谢谢,我试一下.

论坛徽章:
0
4 [报告]
发表于 2007-07-17 15:01 |只看该作者
又出现一个问题,拿fnems的测试用例来说吧.
      就是act 表中存在user相同的好几条记录,表act还存在一个时间字段(time).记录上线时间
现在的目的就是更新chk表中的ip字段,将act 中user相同的,time时间为最新时间的那个sttid付
给 chk表中的ip字段.

mysql> update radcheck as x set ipaddr=(select CallingStationId from radacct as y where x.username=y.username) where x.username in (select username from radacct) and x.sbm='1' and ipaddr='';
ERROR 1242 (21000): Subquery returns more than 1 row

论坛徽章:
0
5 [报告]
发表于 2007-07-17 15:33 |只看该作者
嗯,看看这样行了吧?  自己感觉效率有点低,呵呵,等待版内各高手的答案

create table chk(
  user varchar(8) not null,
  ip   char(15)  not null
);

create table act(
  user  varchar(8) not null,
  sttid char(15)  not null,
  time  int not null
);

insert into act values
('tom','1.1.1.1',1),
('tom','1.1.1.6',2),
('tom','1.1.1.9',4),
('James','1.1.1.2',1),
('Peter','1.1.1.3',1),
('Joe','1.1.1.4',1);
insert into chk values
('tom',''),
('James',''),
('Peter',''),
('Robot','');

select * from act
where (user,time) in
   (select user,max(time) from act group by user);
--  这样select的话,保证选出来的子集都是“时间最大的用户纪录”

update chk as x
set ip=(select sttid from act as y
           where x.user=y.user and (user,time) in
             (select user,max(time) from act group by user)
        )
where x.user in (select user from act);


还有,没有用楼主的命名实例,是为了少敲点键盘,就麻烦楼主自己改一改语句吧

[ 本帖最后由 fnems 于 2007-7-17 15:35 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-07-17 16:10 |只看该作者
UPDATE radcheck,radacct SET radcheck.ipaddr=radacct.CallingStationId WHERE radcheck.username=radacct.username;

ERROR 1054 (42S22): Unknown column 'radacct.username' in 'where clause'

SQL语句中关键字大写是个好习惯,不看错误提示的人做不好程序员

论坛徽章:
0
7 [报告]
发表于 2007-07-17 16:24 |只看该作者
再此非常谢谢fnems 的帮忙, 我自己也试着写了一个.
update chk as x
set ip=(select sttid from (select user,sttid,max(time) time from act group by user)
as y where x.user=y.user) where x.user in (select user from act);

这个语句是要写在配置文件里面的,他是要每十分钟执行一次,所以我想问一下,哪个执行起来更快,不那么耗资源,或者还有没有更简洁更好的写法,希望帮帮我.

还有就是以前一直是用MSSQL,现在要用MYSQL,虽然基本的语句都类似,但是还有不同的,所以就是想了解一下,这两种数据库在语句上有那些不同?写的时候应该注意什么?或者有什么好的忠告???

论坛徽章:
0
8 [报告]
发表于 2007-07-17 17:01 |只看该作者

回复 #6 Namelessxp 的帖子

但是我大写了还是会这样,这应该就是sql server和mysql 的区别吧.
mysql> update radcheck set radcheck.ipaddr=radacct.CallingStationId where radcheck.UserName=radacct.UserName;
ERROR 1054 (42S22): Unknown column 'radacct.UserName' in 'where clause'

论坛徽章:
0
9 [报告]
发表于 2007-07-17 17:04 |只看该作者
原帖由 liudan182 于 2007-7-17 17:01 发表
但是我大写了还是会这样,这应该就是sql server和mysql 的区别吧.
mysql> update radcheck set radcheck.ipaddr=radacct.CallingStationId where radcheck.UserName=radacct.UserName;
ERROR 1054 (42S22): Un ...

这样写是在做什么?

论坛徽章:
0
10 [报告]
发表于 2007-07-17 17:09 |只看该作者
记住一条,用了子查询就避免用in,因为效率很低。

如何避免参见:

http://bbs.chinaunix.net/viewthread.php?tid=960762&extra=page%3D2
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP