MySQL根据时间查询数据

时间:2013-07-22 22:44:10

标签: mysql database

我有以下表格:

create table company
(
    id int(10) unsigned not null auto_increment,
    name varchar(50) not null,
    primary key (id)
);

create table invoice
(
    id int(10) unsigned not null auto_increment,
    company_id int(10) unsigned not null,        
    invoice_number varchar(50) not null,
    paid_date datetime not null,
    paid_amount decimal(10, 2) not null,
    primary key (id),
    foreign key (company_id) references company(id)
);

create table company_rate
(
    id int(10) unsigned not null auto_increment,
    company_id int(10) unsigned not null,
    rate decimal(10, 2) not null,
    effective_date datetime not null,
    primary key (id),
    foreign key (company_id) references company(id)
);

公司表保存公司名称。 发票表包含发票编号,公司获得付款的日期和金额。 公司费率表保存费率有效时的费率和起始日期。 例如,

company_id    rate    effective_date
-------------------------------------
1              100        0000-00-00
1              350        2013-07-01
1              450        2013-07-20
1              500        2014-01-01

如果当前日期是07-03,则费率为350,如果日期为09-21,则费率为450,等等。

我正在尝试根据这些表创建两个函数。

一种是通过选择公司创建发票,并使用html表格或其他任何方式根据生效日期显示当前汇率。

另一个是创建这样的列表:

company_name    invoice_number    rate    paid_amount    paid_date
-------------------------------------------------------------------
Apple           12345             300      300           2013-01-14
Apple           12346             500      400           2013-05-12
Intel           44441             1200     1350          2013-06-01

它打印发票信息,费率由paid_date确定。

尝试过:

SELECT c.name as company_name, i.invoice_number, r.rate, i.paid_amount, i.paid_date 
FROM invoice as i 
    LEFT JOIN company as c on c.id = i.company_id 
    LEFT JOIN (
        SELECT company_id, rate, effective_date 
            FROM  company_rate 
            WHERE effective_date IN (
                SELECT MAX(effective_date) 
                    FROM company_rate 
                        GROUP BY company_id
     )) AS r ON r.company_id = c.id 
         AND r.effective_date <= i.paid_date

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

由于您在MySQL标签下发布此内容,我认为您正在寻找“纯sql”解决方案,所以这是我的建议。我会帮你解决第一种情况,并让你解决第二种情况(一旦你理解了如何解决第一种情况,应该很容易)。

获取最新费率。

因此,您希望根据“当前”日期获取最新费率。您可以使用子查询来实现此目的:

select 
    cr.*
from 
    company_rate as cr
    inner join (
        select company_id, max(date) as maxDate
        from company_rate
        where company_id = @yourCompanyId  -- Set (or replace) the variable with the desired value
          and date <= @yourDate  -- Set (or replace) the variable with the desired value
        group by company_id
    )

答案 1 :(得分:0)

可能的解决方案是使用相关子查询

SELECT c.name, invoice_number, 
       (SELECT rate
          FROM company_rate 
         WHERE company_id = i.company_id
           AND effective_date <= i.paid_date
         ORDER BY effective_date DESC
         LIMIT 1) rate,
       paid_amount,
       paid_date
  FROM invoice i JOIN company c
    ON i.company_id = c.id 

示例输出:

|     NAME | INVOICE_NUMBER | RATE | PAID_AMOUNT |                      PAID_DATE |
-----------------------------------------------------------------------------------
| Company1 |          12345 |  100 |         300 | January, 14 2013 00:00:00+0000 |
| Company1 |          12346 |  350 |         400 |     May, 12 2013 00:00:00+0000 |
| Company2 |          44441 | 1200 |        1350 |    June, 01 2013 00:00:00+0000 |

这是 SQLFiddle 演示