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)。 where子句中不能使用字段「contract_id」。有沒有辦法在mySQL中做到這一點?如何在MySQL
請幫忙。謝謝!
你的問題很困惑解釋清楚? – Sathish
我需要執行上面的查詢,結果看起來像上面給出的。 但我需要得到與一個「contract_id」相關的結果。 8. 我想要得到的所有結果與「Contract_id」等於8。但是我不能在where子句中使用「contract_id」。因爲它取決於查詢的結果。 – user2947024