MySQL error 1064 - what is causing it? - mysql

I have two similar queries, and the second one is throwing a 1064 error and I can't figure out why. Do you see the issue?
select * from node
join field_data_field_taxonomytopics as tt
on node.nid = tt.entity_id
where tt.bundle = 'magazine_article' and tt.entity_id = 61928;
SELECT
FROM
node node
INNER JOIN field_data_field_taxonomytopics field_data_field_taxonomytopics ON node.nid = field_data_field_taxonomytopics.entity_id
WHERE (field_data_field_taxonomytopics.bundle = 'magazine_article') AND (field_data_field_taxonomytopics.entity_id = '61928')

TL;DR Error #1064 means that MySQL can't understand your command. To fix it:
Read the error message. It tells you exactly where in your command
MySQL got confused. Check the manual. By comparing against what MySQL
expected at that point, the problem is often obvious. Check for
reserved words. If the error occurred on an object identifier, check
that it isn't a reserved word (and, if it is, ensure that it's
properly quoted).
I am guessing the issue here is that in the FROM clause you wrote the table name twice "node node", in addition, you are not selecting anything.
SELECT *
FROM
node node
I hope that will fix the error.

Related

MySQL - Where is the Error in this Query? (Error 1064)

I'm trying to execute the query below but WorkBench keeps complaining about a syntax error (Error 1064). I don't know what the error is because even WorkBench highlights each bracket pair and so I can't say there is a missing bracket. Please help.
SELECT
If(Right(Trim(`tbloldfurniture`.`NotesOnOldness`), 4) = 'susp', Substring(Trim(`tbloldfurniture`.`NotesOnOldness`), 1, Char_Length(Trim(Lower(`tbloldfurniture`.`NotesOnOldness`)) - 1)) ,Substring(Trim(Lower(`tbloldfurniture`.`NotesOnOldness`))))
FROM `tbloldfurniture`;
This is the same query broken into its separate parts to aid readability.
SELECT
If(
Right(Trim(`tbloldfurniture`.`NotesOnOldness`), 4) = 'susp',
Substring(Trim(`tbloldfurniture`.`NotesOnOldness`), 1, Char_Length(Trim(Lower(`tbloldfurniture`.`NotesOnOldness`)) - 1)) ,
Substring(Trim(Lower(`tbloldfurniture`.`NotesOnOldness`)))
)
FROM `tbloldfurniture`;
MySQL's substring() function expects at least two parameters and you're feeding it just one in Substring(Trim(Lower(tbloldfurniture.NotesOnOldness))).
Also, you're probably doing something wrong, as you're deducting 1 from a string in Char_Length(Trim(Lower(tbloldfurniture.NotesOnOldness)) - 1).
And yeah, SQL is notoriously terrible with its error messages. Confusing as heck.
The 1064 error is a syntax error. This means the reason there’s a problem is because MySQL doesn’t understand what you’re asking it to do. Cant see an issue with what you have put tho. If your query attempts to reference information in a database and can’t find it, that could be the issue ?

SAL Query error: Unexpected keyword. (near INNER JOIN) Unrecognized statement type. (near INNER JOIN)

the first post on here so apologies if I haven't formatted things properly or if I'm not entirely clear, but I was wondering if someone may be able to assist with an error I'm facing currently.
To give some context, I'm trying to run an SQL query through my phpMyAdmin, in order to fix a customer error we were receiving on-site, after changing our Stripe accounts
WooCommerce support had provided me with the following link:
https://woocommerce.com/document/stripe-fixing-customer-errors/#section-3
However, when I try this in my phpMyAdmin, I get the following error:
Unexpected keyword. (near INNER JOIN) Unrecognized statement type.
(near INNER JOIN)
SQL Error
The query that they had asked me to run is seen here:
DELETE FROM `wp_usermeta`
WHERE meta_key IN ( '_stripe_customer_id', '_stripe_source_id', '_stripe_card_id' );
DELETE tokenmeta FROM `wp_woocommerce_payment_tokenmeta` tokenmeta
INNER JOIN `wp_woocommerce_payment_tokens` ON `wp_woocommerce_payment_tokens`.`token_id` = tokenmeta.`payment_token_id`
WHERE `wp_woocommerce_payment_tokens`.`gateway_id` = 'stripe';
DELETE FROM `wp_woocommerce_payment_tokens` WHERE gateway_id='stripe';
I had spoken to support, who had said that the query is absolutely fine and they could run it on their end, and my developer friends are also a little unsure about this.
Is someone able to shed some light on this and see if I'm doing something wrong? There is a good chance that there is a simple change that's needed that's gone over my head, but I'm just not too sure what it is!

MySQLWorkbench returns 'OK' instead of returning rows

ok so, im trying to generate "TOP 5 name of childcare centres that is popular among the SC and SPR" and i got an "OK" instead of "xxx rows returned" in mySQLWorkbench output. Issit possible for anyone of you to check if my query is correct??
mySQLWorkbench version is 8.0.18
SELECT centre_service.centre_name
FROM centre_service
WHERE centre_service.centre_code = centre.centre_code AND (type_of_citizenship = "SC" OR type_of_citizenship = "SPR") AND centre_name = (
SELECT centre.centre_name
FROM centre
GROUP BY centre_name LIMIT 5
);
This answer does not tackle the errors in the query, but helps to answer the question about why neither an error is seen nor data rows, and what can be done about it.
MySQL Workbench does not handle all types of query errors nicely. Sometimes it just returns "OK" with no rows. I have seen this with, for example, illegal mixed collation errors.
When this happens, just issue this in the query tab:
show errors;
This will return one or more errors, each with a level, a code and a message. For example, my query (details not important) returned "OK" and nothing else. Then show errors gave me this:
Level
Code
Message
Error
1267
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
I cannot say for certain, but it seems like [some?] run-time errors are prone to this problem, rather than syntax errors that are caught immediately.
Try this query. You had 2 mistakes: using reference of a table from subquery in an outer scope (replaced to the inner scope) and using comparison operator to the multiple values (changed to IN).
SELECT centre_service.centre_name
FROM centre_service
LEFT JOIN (
SELECT centre_name
FROM centre
GROUP BY centre_name LIMIT 5
) centre ON centre.centre_name = centre_service.centre_name
WHERE
(type_of_citizenship = "SC" OR type_of_citizenship = "SPR") AND
centre_service.centre_code = centre.centre_code;

Yii2 Sphinx Left join not working

I have been trying to use Yii-Sphinx extension and its working fine when i used a simple query but when i try to use left join then it does not work. It returns the below error. I have tested many queries but not working. I am using Yii-Sphinx extension
SQLSTATE[42000]: Syntax error or access violation: 1064 sphinxql: syntax
error, unexpected IDENT, expecting $end near 'LEFT JOIN specs ON specs.id =
listing.specs_id'
The SQL being executed was: SELECT specs.id, listing.title,listing.specs_id,
listing.reg_no, listing.price, listing.status, listing.featured FROM listing
LEFT JOIN specs ON specs.id = listing.specs_id
Error Info: Array
(
[0] => 42000
[1] => 1064
[2] => sphinxql: syntax error, unexpected IDENT, expecting $end near
'LEFT JOIN specs ON specs.id = listing.specs_id'
)
here is my query
SELECT specs.id, listing.title,listing.specs_id, listing.reg_no, listing.price, listing.status, listing.featured FROM listing LEFT JOIN specs ON specs.id = listing.specs_id
I have solved this issue. Here is the detail for anybody who gets stuck in such an issue. Following is my solution with sphinx query builder available in yii2-sphinx extension:
$q = new Query();
$q->from('listing');
$rows = $q->all();
'listing' is the index from the sphinx config file, Join query can be written in sql_query
What i was doing wrong that i was using the simple query rather than query builder and that solved the issue. For example, join query will not work with the below code.
$sql = 'Select * FROM listing';
$rows = Yii::$app->sphinx->createCommand($sql)->queryAll();
Simple query will be executed but the query with any join will return error.
SphinxSearch itself does not support 'JOIN's. It can't run such queries.
Not a problem with yii2-sphinx as such, its how Sphinx Works.
If writing SphinxQL queries directly, can read the SELECT syntax here:
http://sphinxsearch.com/docs/current.html#sphinxql-select
SELECT statement was introduced in version 0.9.9-rc2. It's syntax is based upon regular SQL but adds several Sphinx-specific extensions and has a few omissions (such as (currently) missing support for JOINs).
In general use QueryBuilder
http://www.yiiframework.com/doc-2.0/yii-sphinx-querybuilder.html
as it only offers 'methods' actully supported by Sphinx.

what is wrong with this line of SQL?

SELECT Batch.NumStud
FROM Batch
WHERE CourseID='$courseid'
INNER JOIN Course
ON Batch.CourseID=Course.CourseID"
an error that says mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>F:\AppServ\www\anNoECourse.php
is shown.This code was written to feed in data to a google chart.
You put SQL in wrong order (JOIN and WHERE are switched):
SELECT Batch.NumStud
FROM Batch INNER JOIN Course
ON Batch.CourseID = Course.CourseID
WHERE Course.CourseID = '$courseid'
It seems, that your query can be simplified (check your data):
select Batch.NumStud
from Batch
where Batch.CourseID = '$courseid'
I think the error is a bit more complex. Due to the fact that your SQL is invalid, you're not getting a result set. This case is not handled correctly by your PHP code!
So in addition to correcting your SQL as the others have suggested, please make sure to handle the case where you get no results or your query results in an error correctly in your PHP code!
The second part to your solution is as follows:
$result = mysql_query(...);
if ($result)
{
while (...)
...
}
This makes sure that mysql_query actually returned a result set and not false, which it does in case of errors (due to your invalid SQL code, but also in other cases). So just fixing your SQL is not enough to make your script error proof.
But again, do no longer use the mysql_.... functions! They are deprecated.