質問

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)
役に立ちましたか?

解決

"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.

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. It doesn't matter what you choose as the alias, it just has to have one.

"Unknown column 'rownum' in 'where clause'"

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. But this is not standard SQL, and MySQL doesn't support it.

So I removed the reference to the implicit ROWNUM column, and replaced it with a LIMIT clause.

他のヒント

Assuming that you have a column named 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:

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

Instead of 3 total queries, you can do this by JOINing. Look into JOINs to see how you can join across the columns you wish to JOIN on from each table.

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帰属
所属していません StackOverflow
scroll top