ActiveRecord - Order by field throws StatementInvalid when calling last - mysql

Creating a activerecord query and calling last which has an order using MYSQL's order by field throws a StatementInvalid exception.
For example:
ruby-1.9.2-p180 > User.order('field(name, \'joe\')').last
ActiveRecord::StatementInvalid: Mysql2::Error: 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 'DESC, "joe") DESC LIMIT 1' at line 1:
SELECT `users`.* FROM `users` ORDER BY field(name DESC, "joe") DESC LIMIT 1
The issue is that activerecord is appending DESC to name inside of the field statement as well as outside.
Is there a better way to order by specific column values with activerecord, or a workaround for this issue?

What's wrong with
User.where("name LIKE ?", \'joe\').last!
If you're just wanting the last record, why bother with order?

Related

SELECT LAST(CustomerName) AS LastCustomer FROM Customers;

I am beginner to mysql, while I practice syntax of LAST() i.e
SELECT LAST(CustomerName) AS LastCustomer FROM CUSTOMERS; I am getting an error.
Error : ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for
the right syntax to use near '(CustomerName) AS LastCustomer FROM
CUSTOMERS' at line 1
the thing is there is nothing on mysql like LAST() function. You can see the comment here where it is said.
You can find the list of mysql functions visiting mysql aggregated functions
It is better to use simple query like the following -
SELECT CustomerName as LastCustomer ORDER BY id DESC LIMIT 1
Hope it helps...:)
Last() is not supported in Mysql so try
SELECT TOP 1 CustomerName FROM Customers ORDER BY CustomerID DESC;
try this
SELECT * FROM CUSTOMERS ORDER BY customer_id DESC LIMIT 1;

MySQL Getting the return of a substraction

We are trying to get the length of a physical exercise by using the timestamps on our sensor data.
We currently have the following query:
SELECT UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp DESC LIMIT 1)
- UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp ASC LIMIT 1)
AS output
(enters added for readability)
When testing this query in phpMyAdmin we get the following error:
#1064 - 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 'SELECT HAAS2.trainingsdata.timestamp FROM HAAS2.trainingdata
WHERE HAAS2.trainin' at line 1
We've tried different ways to write down the query all resulting in the same error. We don't understand where the syntax error lies.
SELECT max(UNIX_TIMESTAMP(timestamp)) -
min(UNIX_TIMESTAMP(timestamp)) AS output
FROM HAAS2.trainingdata
WHERE training_id = 1

sql query order by multiple CAST value int

i'm trying to run this in sql query, but not working. I tried single cast, it works fine, but when i tried to add two cast, it gives error
#1064 - 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 'FROM useradvert WHERE optionsalerent='For sale' ORDER BY cast (rm as signed)' at line 1
This query below works.
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC
THis one doesn't work
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC, cast(salary as signed) ASC
I need to output both rm and salary rm. Both are integer values. Tqs in advance.

SELECT * FROM tablename ORDER BY LIMIT not working

i wanted to display the table values when the user selecting range. The user input saving using String range=request.getParameter();
I put this value in to this mysql query
"SELECT * FROM user_info ORDER BY user_id LIMIT '"+range+"',10";
but it displays a syntax error msg. some problem in '"+range+"' syntax.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''1',10' at line 1 .
You are using quotes for range. So query becomes:
SELECT * FROM user_info ORDER BY user_id LIMIT '1',10
which is wrong.
Try this:
"SELECT * FROM user_info ORDER BY user_id LIMIT "+range+",10";

Need help with correct SQL query

So I asked a similar question yesterday, and got a great response - I'm hoping for the same luck this time around. What I'm trying to create is paged results for a MySQL query. I'm taking advantage of LIMIT. Here is my query.
SELECT
BookingInfo.ClinicID, BookingInfo.BookingDate, BookingInfo.BookingTime,
BookingInfo.Status, PatientBooking.FirstName, PatientBooking.LastName,
PatientBooking.DateOfBirth
FROM BookingInfo
LEFT JOIN PatientBooking
ON BookingInfo.PatientID = PatientBooking.PatientID
WHERE PatientBooking.FirstName = 'Brian'
AND PatientBooking.LastName = 'Lang'
AND (BookingInfo.ClinicID = '1' OR BookingInfo.ClinicID = '2')
LIMIT '0, 20'
ORDER BY BookingInfo.BookingDate DESC
This query does not work (However if I remove the LIMIT '0,20' part it works just fine). Instead I get this error returned: #1064 - 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 ''0, 20' ORDER BY BookingInfo.BookingDate DESC' at line 1
Any ideas from the experts?
Your LIMIT clause should not be enclosed in quotes. It should just read LIMIT 0, 20. Quotes are usually reserved for character strings.
Also, the LIMIT clause must appear after the ORDER BY clause. See SheepSimulator's answer for details.
Ref
I think the limit should come after the order. But what about is not working?
Your ORDER clause should be placed before the LIMIT clause.
Check out the MySQL syntax table for SELECT.
The LIMIT clause should be after the ORDER BY clause.
It's format is LIMIT 0, 20 not LIMIT '0, 20'.
Reference: MySQL SELECT syntax.