求一条联合两个表的数据对某个表中的数据进行更新的HQL或SQL语句
CREATE TABLE `userexpands` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`usercharge` int(11) DEFAULT NULL,
`remain` int(11) NOT NULL DEFAULT '0',
`sentcount` int(11) NOT NULL DEFAULT '0',
`mscount` int(11) NOT NULL DEFAULT '0',
`sent` int(11) NOT NULL DEFAULT '0',
`recharge` int(11) unsigned NOT NULL DEFAULT '0',
`time` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `usercharge` (`usercharge`),
CONSTRAINT `userexpands_ibfk_1` FOREIGN KEY (`usercharge`) REFERENCES `usercharge` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `usercharge` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`balance` int(11) NOT NULL DEFAULT '0',
`sent` int(11) NOT NULL DEFAULT '0',
`expand` int(11) NOT NULL DEFAULT '0',
`cost` int(11) NOT NULL DEFAULT '750',
`note1` varchar(255) DEFAULT NULL,
`note2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
userexpands表和usercharge表的关系是1对多,userexpands表中的usercharge指向usercharge表的主键。
现在想对userexpands表中所有time>mytime,usercharge不为空的记录进行更新。
更新如下:userexpands.sentcount = usercharge.sent, userexpands.sent = usercharge.expand。
请问如何写出这样联合两个表的数据对userexpands表中的数据进行更新的HQL或SQL语句?
本人初接触数据库,用的是Hibernate+MySQL。
我原来的想法是根据这两个表查询出所有符合条件的userexpands,再逐条对返回集合中的记录进行更行,更新的时候加载相应的usercharge数据。但是这样效率不高,所以求一条能够实现上述更新的HQL或SQL语句。