I am writing tests for my database in mysql.
con.query(“SELECT `list`.* FROM `list` WHERE `list`.`place` = 86 AND `list`.`person` = \"#{person_id}\" AND (( (list.state = 'open' AND num=\"#{num}\") || (list.state = ?) )) AND (list.updated_at > \"2018-06-05\") ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC LIMIT 50 OFFSET 1”)
list is a table, and num, place, and person are values of a single item from list. I'm also using this in a ruby script.
I get a syntax error. The error message is:
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 ‘`?) )) AND (list.update_at > “2018-06-05”)) ORDER BY list.person, or`’
I cannot figure out the issue. Please help me.
Look at the ORDER BY part of the query
ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC
list.state should not have an ='closed'
Just try changing the it all to
ORDER BY list.person, list.state DESC, list.updated_at DESC
I think you are using the wrong character for double quotes. You have “. Try " instead.
Related
I want to fetch entire record with distinct field points so I wrote following query but it is not giving desired result.
#top_score_cards = Score.select("DISTINCT points *").order("points DESC , updated_at DESC").where("points != ? And activity_id= ? ",0,params[:#myActivity]).limit(3)
It is throwing following error :
Mysql2::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server versi on for the right syntax to
use near 'FROM scores WHERE (points != 0 And activity_id= '1' )
ORDER BY points DE SC ,' at line 1: SELECT DISTINCT points * FROM
scores WHERE (points != 0 And activity_id= '1' ) ORDER BY poi nts
DESC , updated_at DESC LIMIT 3
The logic of what you're asking for doesn't really make sense, since select will just return the points but you want the whole record.
I think i can guess what it's supposed to be though since i answered another related question earlier. :)
Am i right in thinking that you want one record for the 3 highest unique "points" value in the table, and when there is more than one record with the same points you want the one with the earliest updated_at? if so, then the key to this is group by, not select.
#top_score_cards = Score.group(:points).order("points DESC , updated_at").where("points <> ? and activity_id = ? ",0,params[:activity_id]).limit(3)
I've taken the liberty of fixing lots of syntax errors in your code, so i can't guarantee this this will work or is even what you want.
Then try this instead:
Score.where("points != ? And activity_id= ?",0,params[:#myActivity])
.order("points DESC, updated_at DESC")
.limit(3)
.uniq! { |x| x.points }
Based on the documentation: http://apidock.com/rails/ActiveRecord/QueryMethods/distinct
I am using this line on my MySQL database:
SELECT * FROM `dump` USE INDEX `time_desc` WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1) ORDER BY `time` DESC LIMIT 1;
I cannot figure out why, but the database is returning the following error:
[Error: ER_PARSE_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 'time_desc WHERE nodeId=10048 AND
time<=1377040709 AND valid=1 ORDER BY `ti' at line 1]
Any thoughts as to how I can fix this?
You need parentheses around the index name
SELECT *
FROM `dump` USE INDEX (`time_desc`)
WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1)
ORDER BY `time` DESC
LIMIT 1;
Try like this:-
SELECT * FROM `dump` USE INDEX (`time_desc`) --Use paranthesis here
WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1)
ORDER BY `time` DESC
LIMIT 1;
Check this for reference.
I'm trying to run a MySQL query via WordPress, to bring back a list of posts that I want to delete because they have no "like" votes (using someone else's plugin data). The query works perfectly in phpMyAdmin but gives a syntax error when I run it through WP... and I see absolutely no reason why it would do this.
Here's the query code, which checks for posts over 30 days old that have no corresponding "like" entry in wti_like_post (whether positive or negative):
$novotesquery = "SELECT * FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND $wpdb->posts.ID NOT IN
(SELECT DISTINCT post_id FROM $wpdb->wti_like_post)" ;
$result = $wpdb->get_results($novotesquery);
The syntax error says there's a problem on the last line of the SQL (the SELECT in parentheses): "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 ')' at line 6".
When I run the query in phpMyAdmin (replacing "$wpdb->" with the table prefix), it works a treat. If anyone can tell me why the SQL query will run on the server and not in WP, I'd appreciate it.
Thanks in advance.
Perhaps it's just a matter of defensive parenthesis
$novotesquery = "SELECT * FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = 'post'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND {$wpdb->posts}.ID NOT IN
(SELECT DISTINCT post_id FROM {$wpdb->wti_like_post})" ;
Perhaps you should use $wpdb->query method instead of get_results and finally, perhaps wti_like_posts is not yet declared when your code runs.
What about die($novotesquery) right before "$result" line?
List<Lahetys> last = session.createQuery("from lahetys order by lahetysNro DESC LIMIT 1").list();
and in the log I got:
INFO: Hibernate: select from order by lahetysNro DESC LIMIT 1
WARN: SQL Error: 1064, SQLState: 42000
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 'from order by lahetysNro DESC LIMIT 1' at line 1
What has happend to "from LAHETYS"? What is the best practice to handle that with HQL or/and with SQL?
Another problem:
Lahetys last = (Lahetys)session.createSQLQuery("select * from lahetys order by lahetysNro DESC LIMIT 1").uniqueResult();
session.getTransaction().commit();
and I get a exception:
Ljava.lang.Object; cannot be cast to Lahetys
So I can't cast an object to my Lahetys-object, weird?
Thank you!
Sami
Your HQL query is invalid. LIMIT is not a valid HQL clause. To do that in Hibernate, just do
Query query = session.createQuery("from lahetys order by lahetysNro DESC");
query.setMaxResults(1);
Lahetys last = (Lahetys) query.uniqueResult();
When you're using HQL, you should specify fully qualified className instead of tableName. The same way you should specify propertyName instead of columnName. Also keep in mind that both are case - sensitive.
Looking at your queries & the exception you're getting, I'm assuming that lahetys is your table name & lahetysNro is your column name.
You should use for example: If your Lahetys class is located at com folder:
List<Lahetys> last = session.createQuery("from com.Lahetys order by lahetysNro DESC LIMIT 1").list();
For your 2nd question:
Here you have used SQL instead of HQL.
When you use SQL with hibernate in such a way, it always returns List<Object[]> & not List<Lahetys[]>.
i have a mysql table with the follwoing fields :
id, desc, value, people, amount, weight
in the above order i run the follwoing
update match1 set weight = 5 where desc = 'fat' and id != '6';
follwoing is the error message i get :
**#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 'desc = 'bat' and id = 6' at line 1
can someone please let me know whats wrong with this?
the column desc is a keyword in mysql. use backquotes i.e.
where `desc` = 'fat'