如何在MySQL中过滤未知列的记录集

时间:2014-02-24 04:42:00

标签: mysql select

SELECT
    contract.id AS contract_id,
    contract.currency_code, 
    contract_period.date_from AS rate_period_date_from,
    contract_period.date_to AS rate_period_date_to,
    DATEDIFF(
        contract_period.date_to,
        contract_period.date_from
    ) AS no_of_days 
FROM
    contract
INNER JOIN contract_period ON contract_period.contract_id = contract.id
WHERE
    contract.section_id = 6
AND contract.market_id = 1
AND contract.company_id = 8
AND (
    contract_period.date_from >= '2014-05-01'
    AND contract_period.date_to <= '2014-10-31'
) 
ORDER BY
    contract.id DESC,
    contract_period.date_from ASC

    ->  ;

+-------------+---------------+-----------------------+---------------------+------------+
| contract_id | currency_code | rate_period_date_from | rate_period_date_to | no_of_days |
+-------------+---------------+-----------------------+---------------------+------------+
|           8 | USD           | 2014-07-01            | 2014-07-31          |         30 |
|           8 | USD           | 2014-08-01            | 2014-08-31          |         30 |
|           8 | USD           | 2014-09-01            | 2014-10-31          |         60 |
|           7 | USD           | 2014-05-11            | 2014-05-31          |         20 |
|           7 | USD           | 2014-06-01            | 2014-06-30          |         29 |
+-------------+---------------+-----------------------+---------------------+------------+
    5 rows in set (0.00 sec)

我需要从记录集中获得前3个(有些时候多于或少于3个)。字段“contract_id”不能在where子句中使用。有没有办法在mySQL中执行此操作?

请帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用LIMIT来获取所需的行数。

答案 1 :(得分:0)

如果我理解正确,你需要一个结果的子集,其中contract.id与结果中的第一行相同。

如果是这种情况,则需要具有相同表引用的子查询,其中条件和列按顺序排列。子查询确定最低的contract.id,外部查询选择contract.id等于的结果。

试试这个:

SELECT
    contract.id AS contract_id,
    contract.currency_code, 
    contract_period.date_from AS rate_period_date_from,
    contract_period.date_to AS rate_period_date_to,
    DATEDIFF(
        contract_period.date_to,
        contract_period.date_from
    ) AS no_of_days 
FROM
    contract
INNER JOIN
    contract_period
        ON contract_period.contract_id = contract.id
WHERE
    contract.section_id = 6
    AND contract.market_id = 1
    AND contract.company_id = 8
    AND contract_period.date_from >= '2014-05-01'
    AND contract_period.date_to <= '2014-10-31'
    AND contract.id = (
        SELECT
            MIN(contract.id)
        FROM
            contract
        INNER JOIN
            contract_period
                ON contract_period.contract_id = contract.id
        WHERE
            contract.section_id = 6
            AND contract.market_id = 1
            AND contract.company_id = 8
            AND contract_period.date_from >= '2014-05-01'
            AND contract_period.date_to <= '2014-10-31'
         ORDER BY
            contract.id DESC,
            contract_period.date_from ASC
    )
ORDER BY
    contract.id DESC,
    contract_period.date_from ASC