dポイントプレゼントキャンペーン実施中!

mysqlのDBの日付カラムにunixtimeでデータを保存してあります。
phpで1か月前から1年前までのデータを抽出したいと思っています。

$month = date(strtotime('-4 week', time()));
$year = date(strtotime('-1 year', time()));

select * from items where uptime BETWEEN '$month' AND '$year';

というクエリーだと抽出できなかったので

select * from items where uptime < '$month' and uptime > '$year';

というクエリーにしたら(たぶん)抽出できました。

データがunixtimeの場合、BETWEEN AND は使えないのでしょうか?

A 回答 (3件)

select * from items where uptime BETWEEN curdate() - interval 1 y

ear and curdate() - interval 4 week
    • good
    • 0
この回答へのお礼

上記のcurdateを使ったクエリーだと抽出できなかったのですが、「単に、yearとmonthが逆なだけでは?」というアドバイスで、BETWEENを使うことができました。ありがとうございました!

お礼日時:2020/09/24 16:42

失礼、ミスった


select * from items where uptime BETWEEN curdate() - interval 1 year and select curdate() - interval 4 week
    • good
    • 0

単に、yearとmonthが逆なだけでは?


BETWEEN '$year' AND '$month'

まぁ、わざわざphpから日付をあたえる必要はないですけどね
select * from items where uptime BETWEEN curdate() - interval 1 year and select curdate() - 4 week
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!