0

SQL クエリから最も近い DateTime データのみを表示したいのですが、データは 2 つのテーブルにまたがっています。SocialDatabase.SocialStart > NOW() LIMIT 1あるテーブルから最新のデータを選択するコードTrainingDatabase.SocialStart > NOW() LIMIT 1があり、他のテーブルでも同じことを行うことがわかっています。

では、SocialDatabase テーブルと TrainingDatabase テーブルの組み合わせリストから最も近いデータのみを取得するために、これらをマージするにはどうすればよいでしょうか。

ありがとう

4

2 に答える 2

0

UNION構文を使用できます。

(select SocialStart, Data from SocialDatabase where SocialDatabase.SocialStart > now() limit 1)
union
(select SocialStart, Data from TrainingDatabase where TrainingDatabase.SocialStart > now() limit 1)
order by 1 desc limit 1
于 2013-04-18T00:10:16.083 に答える
0

viewメモリ内に仮想テーブルを作成し、選択を実行できます。

例:

create view tmp as select id, timestamp from SocialDatabase.SocialStart order by timestamp asc limit 1;
insert into tmp select id, timestamp from TrainingDatabase.SocialStart order by timestamp asc limit 1;
select id,timestamp from tmp where timestamp>now() order by timestamp asc limit 1;

..または、Java コードで 2 つの選択と 1 つの比較を行うことで、より簡単に実行できます。

于 2013-04-17T23:59:38.140 に答える