[英]Put an empty field in peewee union select
I'm try to select two table that have some common field. 我试着选择两个有一些共同字段的表。 in raw MySQL query, i can write this: 在原始MySQL查询中,我可以这样写:
SELECT t1.id, t1.username, t1.date FROM table1 as 't1' UNION SELECT t2.id, "const_txt", t2.date FROM table2 as 't2'
In that query ,the username
field is not in table2 and I set const_txt
instead. 在该查询中, username
const_txt
段不在table2中,而是设置const_txt
。
So, in peewee, i want to union two table that have the same above situation. 所以,在peewee中,我想结合两个具有相同情况的表。
class PeeweeBaseModel(Model):
class Meta:
database = my_db
class Table1(PeeweeBaseModel):
id = PrimaryKeyField()
username = CharField(255)
date = DateTimeField()
#other fields ...
class Table2(PeeweeBaseModel):
id = PrimaryKeyField()
date = DateTimeField()
#other fields ...
and then , union two model. 然后,结合两个模型。 something like this: 这样的事情:
u = (
Table1(
Table1.id,
Table1.username,
Table1.date
).select()
|
Table2(
Table2.id,
"const_text_instead_real_field_value",
Table2.date
).select()
).select().execute()
But the const_text
is not accepted by a field and ignore in result query. 但是字段不接受const_text
并在结果查询中忽略。
the question is: How can I define a field that does not exist in my table and set it manually in query? 问题是:如何定义表中不存在的字段并在查询中手动设置?
(And I prefer not using SQL() function.) (我更喜欢不使用SQL()函数。)
thanks. 谢谢。
you can use SQL() in SELECT statement. 您可以在SELECT语句中使用SQL()。
u = (
Table1(
Table1.id,
Table1.username,
Table1.date
).select()
|
Table2(
Table2.id,
SQL(" '' AS username "),
Table2.date
).select()
).select().execute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.