2017-02-03 199 views
-1

即时通讯我试图弄清楚如何在我的where子句中使用多于1行而不使用in()和any()因为我当前的数据库不支持这些功能。目前,我有这个查询MySQL的子查询返回多于1行WHERE子句

select crack_id 
from gndmeas 
where timestamp = (select distinct timestamp 
        from gndmeas 
        where site_id='agb' 
        order by timestamp desc limit 10 
       ); 

子查询包括

+---------------------+ | timestamp | +---------------------+ | 2017-01-13 08:15:00 | | 2017-01-10 07:00:00 | | 2017-01-06 08:30:00 | | 2017-01-03 10:00:00 | | 2016-12-27 09:25:00 | | 2016-12-20 07:30:00 | | 2016-12-13 07:35:00 | | 2016-12-09 08:10:00 | | 2016-12-06 07:40:00 | | 2016-12-02 07:00:00 | +---------------------+

,我想对where子句做与数据的选择查询。是否有可能在不使用mysql的any()和in()函数的情况下执行?

在此先感谢。

+0

您可以使用加入 –

+0

你可以尝试使用'EXISTS'。 –

回答

1

您可以使用连接。

select a.crack_id from gndmeas a join (select distinct timestamp from 
    gndmeas where site_id='agb' order by timestamp desc limit 10) b 
    on a.timestamp=b.timestamp 
0

您至少有两个选项。

使用临时表并首先将时间戳存储在临时表中。 使用临时表进行内部联接以过滤主表中的记录。

如果mysql允许(我不确定),使用嵌套表作为内部联接的一部分。

+0

是的临时表是一个选项,但只有在存储过程的情况下......用户不想使用sp ...他想要单个查询 –

1

尝试使用此连接

select a.crack_id 
from gndmeas a 
inner join 
(select distinct timestamp 
        from gndmeas 
        where site_id='agb' 
        order by timestamp desc limit 10 
       )as b 
on a.timestamp = b.timestamp 
+0

你想要一个'INNER JOIN',而不是'LEFT JOIN',在这里,因为不匹配的记录应该过滤掉。 –

+0

@TimBiegeleisen谢谢 –

0

使用左外连接到表:BY子句

select crack_id 
from gndmeas G 
left outer join gndmeas G1 on G.timestamp=G1.timestamp 
where G.site_id='agb' 
order by G.timestamp desc limit 10 

按组鲜明crack_id:

select crack_id 
from gndmeas G 
left outer join gndmeas G1 on G.timestamp=G1.timestamp 
where G.site_id='agb' 
group by G.crack_id 
order by G.timestamp desc limit 10 
相关问题