Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL查询,从不同表的两列中选择最近的日期时间数据_Php_Android_Mysql_Sql - Fatal编程技术网

Php SQL查询,从不同表的两列中选择最近的日期时间数据

Php SQL查询,从不同表的两列中选择最近的日期时间数据,php,android,mysql,sql,Php,Android,Mysql,Sql,我只想显示SQL查询中最近的日期时间数据,但数据跨越两个表。我有代码SocialDatabase.SocialStart>NOW()LIMIT 1从一个表中选择最新数据,我知道TrainingDatabase.SocialStart>NOW()LIMIT 1对另一个表也会做同样的事情 那么,我如何将这些数据合并在一起,从SocialDatabase和TrainingDatabase表的组合列表中只获取最接近的数据呢 谢谢您可以创建一个视图——它是内存中的一个虚拟表,并在其上运行select 例如

我只想显示SQL查询中最近的日期时间数据,但数据跨越两个表。我有代码
SocialDatabase.SocialStart>NOW()LIMIT 1
从一个表中选择最新数据,我知道
TrainingDatabase.SocialStart>NOW()LIMIT 1
对另一个表也会做同样的事情

那么,我如何将这些数据合并在一起,从SocialDatabase和TrainingDatabase表的组合列表中只获取最接近的数据呢


谢谢

您可以创建一个
视图
——它是内存中的一个虚拟表,并在其上运行select

例如:

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代码进行两次选择和一次比较。

您可以使用语法:


请提供你的数据库模式。除了你的模式,样本数据和期望的结果会很好。我刚刚解决了这个问题,我正要发布我自己的答案。不过我还是会给你的,谢谢
(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