我有 2 张 table
deposits
id |userId | amount| Date
1 | 2 | 150 | 2013-11-22 02:57:00
2 | 3 | 230 | 2013-11-25 03:19:00
withdrawals
id |userId | amount| Date
1 | 2 | 150 | 2013-11-23 02:57:00
2 | 3 | 190 | 2013-11-27 02:27:00
我想创建一个 View ,以这种格式显示两个表中的数据 记录最好按日期字段排序,但这并不重要,因为我可以按日期排序查询 View 。
depositsAndWithdrawal
type | id | userId| amount | Date
deposit | 1 | 2 | 150 | 2013-11-22 02:57:00
withdrawal | 1 | 2 | 150 | 2013-11-23 02:57:00
deposit | 2 | 3 | 230 | 2013-11-25 03:19:00
withdrawal | 2 | 3 | 190 | 2013-11-27 02:27:00
这可能吗?或者我是否需要创建一个新表并使用插入事件向该表添加相关行?
最佳答案
您正在寻找union all
查询。您可以在 MySQL View 中执行此操作:
create view v as
select 'deposit' as which, id, userId, amount, Date
from deposits
union all
select 'withdrawals' as which, id, userId, amount, Date
from withdrawals ;
关于MYSQL创建显示两个表中数据的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21152293/