简体   繁体   English

比较多行嵌套SQL语句返回的数据的1行

[英]Compare 1 row of returned data from multi-row nested SQL statement

I have two tables... EVENT (Primary key is EID) (containing all the general event data) and SINGLE_EVENT (Primary Key is SEID) (containing the information about each individual event related to a particular event ID ie date and time of the individual event, location of the venue etc. 我有两个表... EVENT(主键为EID)(包含所有常规事件数据)和SINGLE_EVENT(主键为SEID)(包含有关与特定事件ID相关的每个事件的信息,即事件的日期和时间)个别活动,场地位置等

In summary I want to find the 'single event' which is happening soonest for each overall event(EID) -- this should return a single event for each unique EID in the SINGLE_EVENTS table I then want to bind the overall EVENT information to the returned results. 总而言之,我想找到对于每个整体事件(EID)最快发生的“单个事件”-这应该为SINGLE_EVENTS表中的每个唯一EID返回一个事件,然后将整体EVENT信息绑定到返回的事件结果。

The problem is that, with the current MySQL statement I have below, I need to select * for the nested query to have all the information it needs to process the query but I also DONT want to select all that information because I only need the SEID from that query result (and not the whole table) 问题是,对于下面的当前MySQL语句,我需要为嵌套查询选择*,以获取处理查询所需的所有信息,但我也不想选择所有这些信息,因为我只需要SEID从该查询结果(而不是整个表)

here is my query (obviously executed without the comments): 这是我的查询(显然没有注释地执行):

<!-- non working outer query...
SELECT SINGLE_EVENT.SEID, EVENT.* FROM EVENT 
INNER JOIN SINGLE_EVENT ON SINGLE_EVENT.EID=EVENT.EID 
WHERE SINGLE_EVENT.SEID IN ( 
-->
<!-- working sub query...
select * from SINGLE_EVENT t
inner join (select eid, min(date) as MinDate from SINGLE_EVENT 
            group by eid) tm 
on t.eid=tm.eid and t.date=tm.MinDate and t.date>=sysdate()
-->
)

I am new to SQL and dont know how best to find this information out from the tables. 我是SQL新手,不知道如何最好地从表中查找此信息。 I feel that I'm very close to it working but I keep getting the message "Operand should contain 1 column(s)" because of the multi-column return value of the sub-query. 我觉得我的工作非常接近,但是由于子查询的多列返回值,我不断收到消息“操作数应包含1列”。

Any help is appreciated. 任何帮助表示赞赏。

In your subquery, change: 在您的子查询中,更改:

select * from SINGLE_EVENT t

to

select SEID from SINGLE_EVENT t

You're effectively telling it to search the whole table for the SEID, which isn't allowed in a subquery. 您实际上是在告诉它在整个表中搜索SEID,这在子查询中是不允许的。

If you specify which column the subquery is looking for matches, that should solve your problem. 如果您指定子查询正在寻找匹配的列,那应该可以解决您的问题。

You can simply join first with your aggregates and then with your real table: 您可以简单地先加入您的聚合,然后再加入您的真实表:

select e.*, se.*
from event e
join 
(
  select eid, min(date) as date 
  from single_event
  where date >= sysdate()
  group by eid
) first_events on first_events.eid = e.eid
join single_event se on se.eid = first_events.eid and se.date = first_events.date;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM