2

Not sure what the error is with the query below?

select  r.request_id, rr.request_result_id,r.date_submitted
from request_results rr 
  inner join requests r on rr.request_id = r.request_id 
  where ((rr.file_size IS NULL) or length(rr.results) = 0) 
     and r.date_submitted >= CURDATE() 
order by r.request_id, rr.request_result_id request_results;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'request_results' at line 1

3
  • 4
    Remove request_results from ORDER BY clause. Commented Oct 7, 2013 at 12:46
  • what is the request_results at the end for? Commented Oct 7, 2013 at 12:47
  • dpriver.com/pp/sqlformat.htm also tells you if there are any syntax errors Commented Oct 7, 2013 at 12:58

6 Answers 6

2

replace

ORDER BY r.request_id, rr.request_result_id, X.request_results;

in the end by

ORDER BY r.request_id, rr.request_result_id, r.date_submitted;

because the columns specified in ORDER BY clause should be one of the columns selected in the SELECT column list.

Sign up to request clarification or add additional context in comments.

Comments

0

I think problem is here

order by r.request_id, rr.request_result_id request_results

what does 'request_results' refers to here ?

change it to

order by r.request_id, rr.request_result_id, r.request_results

1 Comment

request_results is the table itself. Should remove it actually.
0

You missed one comma at the end. Try this one but replace the X with the correct table alias

SELECT r.request_id, rr.request_result_id, r.date_submitted
    FROM request_results rr INNER JOIN requests r ON rr.request_id = r.request_id
    WHERE ((rr.file_size IS NULL) or length(rr.results) = 0) and r.date_submitted >= CURDATE()
    ORDER BY r.request_id, rr.request_result_id, X.request_results;

Comments

0

You have the table name added at the end of query for no reason

    SELECT r.request_id,
       rr.request_result_id,
       r.date_submitted
FROM   request_results rr
       INNER JOIN requests r
               ON rr.request_id = r.request_id
WHERE  ( ( rr.file_size IS NULL )
          OR Length(rr.results) = 0 )
       AND r.date_submitted >= Curdate()
ORDER  BY r.request_id,
          rr.request_result_id

For queries like these, use the SQL formatters if you don't want to mess yourself. the one I use is http://www.dpriver.com/pp/sqlformat.htm

Comments

0

Remove request_results from the end, it's a name of a table.

select 
    r.request_id, rr.request_result_id, r.date_submitted
from
    request_results rr
        inner join
    requests r ON rr.request_id = r.request_id
where
    ((rr.file_size IS NULL)
        or length(rr.results) = 0)
        and r.date_submitted >= CURDATE()
order by r.request_id , rr.request_result_id;

And try to write cleaner, well formatted code (or use auto-format option like the one you have in MySQL workbench)

Comments

0

In order by you missing a comma Try this code

select  r.request_id, rr.request_result_id,r.date_submitted
from request_results rr 
  inner join requests r on rr.request_id = r.request_id 
  where ((rr.file_size IS NULL) or length(rr.results) = 0) 
     and r.date_submitted >= CURDATE() 
order by r.request_id, rr.request_result_id, r.date_submitted;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.