查看: 1099|回复: 12
|
小小的错误? sql statement
[复制链接]
|
|
我用以下的code可以select到我要的资料
SELECT ap.insert_date FROM auction_properties ap
WHERE ap.insert_date =
(select min(insert_date) FROM auction_properties i
where i.code = ap.code
AND i.no = ap.no
AND i.commission = ap.commission
AND i.belongto = ap.belongto)
但我一换去 DELETE FROM 就出错了,为何呢?
DELETE FROM auction_properties ap
WHERE ap.insert_date =
(select min(insert_date) FROM auction_properties i
where i.code = ap.code
AND i.no = ap.no
AND i.commission = ap.commission
AND i.belongto = ap.belongto) |
|
|
|
|
|
|
|
发表于 10-10-2006 11:56 AM
|
显示全部楼层
DELETE auction_properties
FROM auction_properties ap
WHERE ap.insert_date =
(select min(insert_date) FROM auction_properties i
where i.code = ap.code
AND i.no = ap.no
AND i.commission = ap.commission
AND i.belongto = ap.belongto) |
|
|
|
|
|
|
|
楼主 |
发表于 10-10-2006 09:28 PM
|
显示全部楼层
不对啊。。
DELETE 后面不该出现 table name的
如: DELETE FROM user_table |
|
|
|
|
|
|
|
发表于 11-10-2006 11:58 PM
|
显示全部楼层
原帖由 counterking 于 10-10-2006 21:28 发表
不对啊。。
DELETE 后面不该出现 table name的
如: DELETE FROM user_table
Err.. 为何不能。。 你有试吗? |
|
|
|
|
|
|
|
发表于 14-10-2006 03:01 PM
|
显示全部楼层
delete record只需要define table name 和criteria就可以了
e。g:DELETE FROM TABLE_NAME
where 。。。。 |
|
|
|
|
|
|
|
楼主 |
发表于 15-10-2006 07:14 PM
|
显示全部楼层
对! 你说的没错!但我的情况是必须先找出同样的code里, insert_date最旧的record才delete... 所以才需要
WHERE ap.insert_date =
(select min(insert_date) FROM auction_properties i
where i.code = ap.code
AND i.no = ap.no
AND i.commission = ap.commission
AND i.belongto = ap.belongto) |
|
|
|
|
|
|
|
发表于 16-10-2006 09:37 AM
|
显示全部楼层
查过,二楼的可行,楼主还是先试过再说吧
(参考 MSSQL HELP)
[ 本帖最后由 神仙祖宗 于 16-10-2006 09:48 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 16-10-2006 03:54 PM
|
显示全部楼层
DELETE auction_properties
FROM auction_properties ap
WHERE ap.insert_date =
(select min(insert_date) FROM auction_properties i
where i.code = ap.code
AND i.no = ap.no
AND i.commission = ap.commission
AND i.belongto = ap.belongto)
我执行以上的sql statement出现以下的error msg (我用mysql)
unknown table "auction_properties" in MULTI DELETE |
|
|
|
|
|
|
|
楼主 |
发表于 16-10-2006 04:02 PM
|
显示全部楼层
其实我只是想要delete相同的资料, 比如说:
No Code No Date
1 A9 01 2006-10-14
2 A5 02 2006-10-14
3 A9 01 2006-10-24
由于 record 1 和 record 3 是duplicated的(code = A9, no = 01)
而record 1比较旧(date = 2006-10-14) 所以必须delete...
这样的sql statement 该如何写。。
我的方法有错吗? |
|
|
|
|
|
|
|
发表于 18-10-2006 12:37 AM
|
显示全部楼层
原帖由 counterking 于 16-10-2006 04:02 PM 发表
其实我只是想要delete相同的资料, 比如说:
No Code No Date
1 A9 01 2006-10-14
2 A5 02 2006-10-14
3 A9 01 2006-10-24
由于 record 1 和 record 3 是 ...
这样做好了, 简单快速.
1. 先把资料 export 去别的 table, 例如 TMP_A, 用 SELECT GROUP BY 的方法来确保你每一个 date 只拿一个.
2. 把这个 table 清空, 然后再从 TMP_A import 回来, 就可以了.
提醒你, 做这些东西前要 backup 你的资料库. |
|
|
|
|
|
|
|
楼主 |
发表于 18-10-2006 10:56 AM
|
显示全部楼层
#statement1
create table tmp_for_delete select code, max(date) date from product group by code
#statement2
delete product from product p join tmp_for_delete d on p.code = d.code and p.date < d.date
#statement3
drop table tmp_for_delete
是这样子吧。。。 欢迎赐教 |
|
|
|
|
|
|
|
发表于 18-10-2006 11:09 PM
|
显示全部楼层
原帖由 counterking 于 18-10-2006 10:56 AM 发表
#statement1
create table tmp_for_delete select code, max(date) date from product group by code
#statement2
delete product from product p join tmp_for_delete d on p.code = d.code and p.date &l ...
你自己测试了以上的 code 吗? |
|
|
|
|
|
|
|
发表于 18-10-2006 11:44 PM
|
显示全部楼层
delete tbTmp
from tbTmp a, (select Code, No, count(*) as 'Cnt', min(insert_date) as 'MinDate'
from tbTmp
group by Code, No) b
where a.Code = b.Code and a.No = b.No and b.Cnt > 1 and a.insert_date = b.MinDate |
|
|
|
|
|
|
| |
本周最热论坛帖子
|