如果行计数= 0,则运行查询

时间:2015-10-06 06:41:38

标签: mysql rowcount ifnull

我创建了3个表:播放列表,跟踪列表和& xmlexport ,我用它来存储我的播放列表。为了避免复制播放列表中的任何项目,我使用了从播放列表中插入的曲目列表。我将我的tracklist的第一行插入到我的xmlexport中,然后我以.xml文件的形式导出要播放的曲目以供我的播放器读取。每次这个过程发生时我都会从轨道列表中删除一行,所以当表格播放最后一个项目时,它会变为空白&我需要运行一个查询来更新播放列表表中的tracklist表。这就是我一直在玩的但是它不能正常工作。

       INSERT INTO dms.test_trk
       SELECT * FROM dms.test_ply
       WHERE row_count() <= 0;

2 个答案:

答案 0 :(得分:1)

With your statement

 INSERT INTO dms.test_trk
 SELECT * FROM dms.test_ply
 WHERE row_count() <= 0;

what exactly do you expect to happen? I would have thought that the result from the last operation (i.e. the one before executing this statement) would be compared with zero and accordingly rows are retrieved from test_ply or not.

You, however, seem to think that row_count somehow tells you whether there are records in test_trk.

Both assumptions seem wrong. My testing indicates that row_count is reset to -1 when starting an INSERT, so you cannot use this function inside an INSERT statement at all.

What you are looking for instead is a simple EXISTS clause:

insert into test_trk
select * from test_ply where not exists (select * from test_trk);

答案 1 :(得分:0)

我认为您需要一个存储过程来检查test_trk中的count(*)是否为0.如果是,请运行:

 INSERT INTO dms.test_trk
 SELECT * FROM dms.test_ply

Thisthis可以帮助您走上正确的道路。