【问题标题】:Mysql Join and OrderMysql 加入和排序
【发布时间】:2011-04-09 18:27:00
【问题描述】:

我有 3 个表,它们的列相同,但数据不同(存款、取款、转账)

如果不存在则创建表 withdrawals (id int(11) 不是 NULL 自动增量,user varchar(12) 默认 NULL,amount double(12,2) 默认 NULL,date 时间戳 NULL 默认 CURRENT_TIMESTAMP,time varchar(50) 默认为 NULL,type varchar(50) 默认为 NULL,status varchar(1) 默认 '0', notes 主键 (id) 引擎=MyISAM 默认 CHARSET=latin1 AUTO_INCREMENT=1 ;

我想将所有 3 个表格合并为 1 个表格,并按所有日期对它们进行排序,并且仅当 user='$user' 时才显示一行

Example if table 1 has this data
id  user    amount  date        time    type            status          notes
2   vveer   142.00  2010-04-01 03:05:01 PayPal              2          deposit
3   test11  116.00  2010-04-01 10:33:23 PayPal              2          deposit

Example if table 2 has this data
id  user    amount  date        time    type            status          notes
3   vveer   142.00  2009-04-01 03:05:01 PayPal              2          withdraw
7   test11  112.00  2010-04-12 10:33:23 PayPal              2          withdraw

Example if table 3 has this data
id  user    amount  date        time    type            status          notes
5   test11  133.00  2010-03-01 10:33:23 PayPal              2          transfer
7   test11  123.00  2010-02-01 10:33:23 PayPal              2          transfer

I want to show all data for user test11 ordered by dates(is like putting all the dates together and ordering by 1 date column ) .
3   test11  112.00  2010-04-12 10:33:23 PayPal              2          withdraw
7   test11  116.00  2010-04-01 10:33:23 PayPal              2          deposit
5   test11  133.00  2010-03-01 10:33:23 PayPal              2          transfer
5   test11  123.00  2010-02-01 10:33:23 PayPal              2          transfer

【问题讨论】:

  • 为什么一开始就不是这样的架构?您应该只有一个事务表和一个类型列,而不是 3 个不同的表。

标签: mysql join sql-order-by


【解决方案1】:

如果我理解您的问题,这种情况就是 UNION 运算符的设计目的。请参阅http://www.w3schools.com/sql/sql_union.asp 了解更多信息。

【讨论】:

    猜你喜欢
    • 2017-10-26
    • 2013-04-04
    • 2011-07-14
    • 2011-12-05
    • 2012-03-01
    • 1970-01-01
    • 2013-11-08
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多