MYSQL把查询到数据新建成一张表

create table VisitRemind as (SELECT a.sourceType,a.sourceId,a.gender,a.nowAddress,a.diseaseType,a.dataBirth,a.VISIT_DATE,a.patientId,a.tel,a.`name`,a.dutyUserUuid,a.sourceTable,a.mbType,b.nextDate from VIEWRESIDENTHIST a 
            LEFT JOIN ( SELECT sourceType, patientId , MAX(NEXT_DATE) nextDate FROM VIEWRESIDENTHIST GROUP BY sourceType, patientId)  b 
            on a.sourceType = b.sourceType and  a.patientId = b.patientId and a.NEXT_DATE = b.nextDate where b.nextDate is not null) 

1.create table 表名 as sql语句;

2.更新版本

create table VisitRemind as(            
SELECT
 a.sourceType,
 a.sourceId,
 a.gender,
 a.nowAddress,
 a.diseaseType,
 a.dataBirth,
 a.VISIT_DATE,
 a.patientId,
 a.tel,
 a.`name`,
 a.dutyUserUuid,
 a.sourceTable,
 a.mbType,
 a.INPUT_USER_ID,
 a.DUTY_USER_ID,
 a.NEXT_DATE 
FROM VIEWRESIDENTHIST a 
where 1=1
 and (sourceType, patientId,NEXT_DATE) in ( SELECT sourceType, patientId, MAX( NEXT_DATE ) FROM VIEWRESIDENTHIST GROUP BY sourceType, patientId ))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,用于根据一个数据建立一个类: ```python import sqlite3 class MyTable: def __init__(self, db_file): self.conn = sqlite3.connect(db_file) self.cursor = self.conn.cursor() self.columns = [] self.primary_key = None self.table_name = None def create(self, table_name): self.table_name = table_name self.cursor.execute(f"SELECT * FROM {table_name} LIMIT 1") self.columns = [description[0] for description in self.cursor.description] self.primary_key = self.get_primary_key() def get_primary_key(self): self.cursor.execute(f"PRAGMA table_info({self.table_name})") for row in self.cursor.fetchall(): if row[5]: return row[1] return None def select_all(self): self.cursor.execute(f"SELECT * FROM {self.table_name}") return self.cursor.fetchall() def insert(self, **kwargs): columns = ",".join(kwargs.keys()) values = ",".join([f"'{value}'" for value in kwargs.values()]) self.cursor.execute(f"INSERT INTO {self.table_name} ({columns}) VALUES ({values})") self.conn.commit() def update(self, primary_key_value, **kwargs): set_clause = ",".join([f"{column}='{value}'" for column, value in kwargs.items()]) self.cursor.execute(f"UPDATE {self.table_name} SET {set_clause} WHERE {self.primary_key}='{primary_key_value}'") self.conn.commit() def delete(self, primary_key_value): self.cursor.execute(f"DELETE FROM {self.table_name} WHERE {self.primary_key}='{primary_key_value}'") self.conn.commit() ``` 在上面的代码中,我们使用了 SQLite3 来连接数据库,并定义了一个名为 `MyTable` 的类。在 `__init__` 方法中,我们打开了一个数据库连接,并创建了一个游标对象。我们还定义了一些属性,包括列名、主键名称和名。 在 `create` 方法中,我们传递一个名作为参数,并使用 `SELECT * FROM` 语句来获取的第一行数据。我们从游标对象的 `description` 属性中获取列名,并使用 `get_primary_key` 方法来获取主键名称。 在 `get_primary_key` 方法中,我们使用 `PRAGMA table_info` 语句来获取的列信息,然后使用循环来查找主键列。如果找到了主键列,则返回其名称。 在 `select_all` 方法中,我们使用 `SELECT * FROM` 语句来获取中的所有数据,并使用游标对象的 `fetchall` 方法来返回查询结果。 在 `insert` 方法中,我们使用 `INSERT INTO` 语句来将一行数据插入到中。 在 `update` 方法中,我们使用 `UPDATE` 语句来更中的一行数据。 在 `delete` 方法中,我们使用 `DELETE FROM` 语句来从中删除一行数据。 使用上面的代码,我们可以轻松地创建一个类来操作数据库。例如,如果我们有一个名为 `users` 的,我们可以使用以下代码来创建一个 `User` 类: ```python class User(MyTable): def __init__(self, db_file): super().__init__(db_file) self.create("users") ``` 然后,我们可以使用以下代码来操作该: ```python user = User("mydatabase.db") user.insert(name="Alice", age=25) user.update("1", age=26) user.delete("1") print(user.select_all()) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值