如下:
使用多表联合查询,其中b表为一条n=0的数据
select (@n:=@n+1) as id, a.* from security_principal a, (select @n:=0) b limit 2
p s:
使用select赋值时,用':=',例如select @aaa:=1024,查询结果为1024
实际使用的场景,是将添加序号列后的数据,用python读取出来,但是会存在一个问题:数据库存的是整数1,但python读取出来的是小数1.0(导入的数据库包版本较低导致),自动化测试断言时会断言失败,如下:
解决方案:在python读取出数据后,进行数据类型转换,如下:
# 执行sql查询语句 - 查询列表数据 def execute_sql_search_listAll(self, sql): self.cur.execute(sql) list_db = [] for n in range(self.cur.rowcount): row = list(self.cur.fetchone()) row[0] = str(int(row[0])) # 将第一列转换为整数,再转换为字符串类型。不然会是小数1.0 list_db.append(row) return list_db