简体   繁体   English

我的SQL查询有什么问题? 它不会获取数据

[英]What's wrong with my SQL query ?? It won't fetch data

I'm basically trying to fetch the next three future events including today from the database m below is my query and it says that derived tables must have an alias , I'm new to this .. what would be the fix ? 我基本上是想从数据库中获取包括今天在内的下三个未来事件,下面是我的查询,它说派生表必须有一个别名,我是新来的..解决方法是什么?

select * from events where event_start_date in
   (select event_start_date from
          (select distinct event_start_date from events
                  where event_start_date >= '2014-05-02' order by event_start_date)
    WHERE rownum <= 3)

Assuming that you have a column named rownum : 假设您有一个名为rownum的列:

select * from events where event_start_date in
  (select distinct event_start_date
  from events
  where event_start_date >= '2014-05-02' AND rownum <= 3
  order by event_start_date) T

EDIT: 编辑:

If you are trying something like rownum as in oracle, you can use LIMIT : 如果您尝试使用像oracle中的rownum方法,则可以使用LIMIT

select * from events where event_start_date in
  (select distinct event_start_date
  from events
  where event_start_date >= '2014-05-02'
  order by event_start_date
  LIMIT 3) T

"derived tables must have an alias" “派生表必须具有别名”

That error means that when you use a subquery in the FROM clause, it's called a derived table and you must declare an alias for that. 该错误意味着在FROM子句中使用子查询时,该子查询称为派生表,并且必须为此声明一个别名。

Here's a simpler form of your query that should work: 这是应该工作的简单查询形式:

SELECT events.* FROM events
JOIN (SELECT DISTINCT event_start_date FROM events
      WHERE event_start_date >= '2014-05-02'
      ORDER BY event_start_date LIMIT 3) AS d USING (event_start_date)

I provided an alias for the derived table where you see AS d . 我为派生表提供了别名,您在其中看到AS d It doesn't matter what you choose as the alias, it just has to have one. 选择什么别名都没有关系,只需要一个别名即可。

"Unknown column 'rownum' in 'where clause'" “'where子句'中的未知列'rownum'”

ROWNUM is an Oracle-specific feature that automatically provides an imaginary column to every result set, with an integer corresponding to the row number of that result set. ROWNUM是Oracle特定的功能,它会自动为每个结果集提供一个假想的列,其整数对应于该结果集的行号。 But this is not standard SQL, and MySQL doesn't support it. 但这不是标准的SQL,而MySQL不支持它。

So I removed the reference to the implicit ROWNUM column, and replaced it with a LIMIT clause. 因此,我删除了对隐式ROWNUM列的引用,并将其替换为LIMIT子句。

Instead of 3 total queries, you can do this by JOINing. 您可以通过JOINing来代替3个查询。 Look into JOINs to see how you can join across the columns you wish to JOIN on from each table. 查看JOIN,以了解如何从每个表中跨入希望加入的列。

select * from events where event_start_date in
   (select event_start_date from
          (select distinct event_start_date,rownum from events
                  where event_start_date >= '2014-05-02' order by event_start_date) as t1
    WHERE rownum <= 3)

This will help you. 这将为您提供帮助。

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

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