Flask-SQLAlchemy。使用 session.query 在一个 table 中打印与另一个 table 中写入的 id 相对应的文本
Flask-SQLAlchemy. Using session.query to print a text in one table corresponding to its id written in another table
我是 SQL 的新手。
我的 MySQL 数据库中有 2 个 table 'doc' 和 'code'。
文档中的一列是 code_id.
在从 'doc' table 获取所有数据的查询过程中,我希望 code_id 被 table 中的列 'text' 替换'code'对应'code_id'
中的id号
示例:
Table-文档
id
文字
code_id
1
水
3
2
空气
1
Table-代码
id
文字
1
跳伞
2
羽毛球
3
游泳
'doc' table 列 'code_id' 中的条目为 3。'text' 列中的数据在 table [=93] 中的 id 为 3 =] 是 'Swimming'。当我通过代码 result=db.session.query(doc)
检索此信息时,我想将 '3' 替换为 'Swimming' 并以这种格式获取数据。
获得此结果的查询是什么?
如果我理解正确的话,您需要一个简单的连接:
请记住,text 是 MySQL 上的保留字,您不应使用保留字命名您的列。
create table doc (
id int(9) not null auto_increment,
`text` varchar(30),
code_id int(9) not null,
Primary key id(`id`) );
insert into doc values (1,'Water',3), (2,'Air',1) ;
create table code (
id int(9) not null auto_increment,
text varchar(30),
Primary key sid(`id`) );
insert into code values (1,'Skydiving'), (2,'Badminton'), (3,'Swimming') ;
您的查询如下:
SELECT doc.*, code.text from doc inner join code on doc.code_id= code.id;
我让 code_id
在 select
查询中只是为了解释,你可以从你的查询中删除它。
我是 SQL 的新手。
我的 MySQL 数据库中有 2 个 table 'doc' 和 'code'。 文档中的一列是 code_id.
在从 'doc' table 获取所有数据的查询过程中,我希望 code_id 被 table 中的列 'text' 替换'code'对应'code_id'
中的id号示例: Table-文档
id | 文字 | code_id |
---|---|---|
1 | 水 | 3 |
2 | 空气 | 1 |
Table-代码
id | 文字 |
---|---|
1 | 跳伞 |
2 | 羽毛球 |
3 | 游泳 |
'doc' table 列 'code_id' 中的条目为 3。'text' 列中的数据在 table [=93] 中的 id 为 3 =] 是 'Swimming'。当我通过代码 result=db.session.query(doc)
检索此信息时,我想将 '3' 替换为 'Swimming' 并以这种格式获取数据。
获得此结果的查询是什么?
如果我理解正确的话,您需要一个简单的连接: 请记住,text 是 MySQL 上的保留字,您不应使用保留字命名您的列。
create table doc (
id int(9) not null auto_increment,
`text` varchar(30),
code_id int(9) not null,
Primary key id(`id`) );
insert into doc values (1,'Water',3), (2,'Air',1) ;
create table code (
id int(9) not null auto_increment,
text varchar(30),
Primary key sid(`id`) );
insert into code values (1,'Skydiving'), (2,'Badminton'), (3,'Swimming') ;
您的查询如下:
SELECT doc.*, code.text from doc inner join code on doc.code_id= code.id;
我让 code_id
在 select
查询中只是为了解释,你可以从你的查询中删除它。