Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私のMySQLデータベースには、挿入日を含むレコードがあります(datetimeタイプの列の1つとして)。 いくつかのwhere句の制約に加えて、指定された日付より上のすべてのレコード (指定された日付から現在まで) と、指定された日付に最も近いレコードを 1 つだけ選択したいと考えています。 MySQLを効率的に使用してこれを達成する方法はありますか? count と rank を使用することも考えましたが、非常に効率が悪いと思います。
datetime
where
もしかしてこういうこと?
(SELECT * FROM mytable WHERE mydate >= xxx) UNION (SELECT * FROM mytable WHERE mydate < xxx ORDER BY mydate DESC LIMIT 1)