将a表的数据库接近一天的开始的点存入表b的begin字段,将a表的数据库接近一天的结束的点存入表b的end字段, 这两点 在 b表内是同一行的。物联网远传设备都是一点一点的来数据的。本来想在服务器接收的时候自动处理的。但是担心不稳定因素会采集不到点
解决方案 »
- C++使用mysql更新二进制数据
- 关于Mysql性能的问题,在线100分等!
- mysql_real_query() 多线程封装的问题
- 商品属性搜索 sql
- access denied for user ''@'localhost'(using password:NO)
- mysqldump 。。dump了几天了。。 0.0~
- 请教mysql中COUNT的用法 ,急
- ASP连接MYSQL编码问题
- 程序中怎样调用mySql的存储过程并取得输出参数?????????????
- mysql如何写多次查询语句?
- 新手求助-mysql 简单语法DELIMITER 问题--errno 1064
- mysql数据库多表查询
参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
没看明白你的问题是只存一条数据,begin字段是开始时间,end字段存储结束时间吗?
-- bv是begin值,ev是end值,类型之类请自己调整
MariaDB [demo]> create table a (id int primary key, bv int);
Query OK, 0 rows affected (0.28 sec)MariaDB [demo]> create table a (id int primary key, ev int);
ERROR 1050 (42S01): Table 'a' already exists
MariaDB [demo]> create table b (id int primary key, ev int);
Query OK, 0 rows affected (0.09 sec)MariaDB [demo]> insert into a values(1, 1),(2, 2);
Query OK, 2 rows affected (0.18 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [demo]> insert into b values(1, 10),(2, 20);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0-- 直接用CTAS最快,也没有锁问题
MariaDB [demo]> create table c as select * from a join b using (id);
Query OK, 2 rows affected (0.28 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [demo]> select * from c;
+----+------+------+
| id | bv | ev |
+----+------+------+
| 1 | 1 | 10 |
| 2 | 2 | 20 |
+----+------+------+
2 rows in set (0.00 sec)-- 重用表,省一步重写create
MariaDB [demo]> delete from c;
Query OK, 2 rows affected (0.01 sec)-- insert into ... select ... 会有锁
MariaDB [demo]> insert into c select * from a join b using (id);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [demo]> select * from c;
+----+------+------+
| id | bv | ev |
+----+------+------+
| 1 | 1 | 10 |
| 2 | 2 | 20 |
+----+------+------+
2 rows in set (0.00 sec)MariaDB [demo]> delete from c;
Query OK, 2 rows affected (0.01 sec)-- 模拟在表a中留出一个null的ev字段
MariaDB [demo]> insert into c select a.*, null from a;
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [demo]> select * from c;
+----+------+------+
| id | bv | ev |
+----+------+------+
| 1 | 1 | NULL |
| 2 | 2 | NULL |
+----+------+------+
2 rows in set (0.00 sec)-- 用update set = (select ...),效率比较
MariaDB [demo]> update c set ev = (select ev from b where b.id = c.id);
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0MariaDB [demo]> select * from c;
+----+------+------+
| id | bv | ev |
+----+------+------+
| 1 | 1 | 10 |
| 2 | 2 | 20 |
+----+------+------+
2 rows in set (0.00 sec)