I am executing the follow mysql query and getting error saying wrong syntax.
SELECT COUNT(*) FROM PS.INFO WHERE IPADDRESS='1.1.1.1' AND ID='YYY' AND (TYPE='PAID' 0R TYPE='FREE') AND EXPIRYTIME IS NULL;
Please help me out with the correct syntax?
Your wrote 0R (zero-R) instead of OR (O-R).
TYPE='PAID' 0R TYPE='FREE'
OR
appears to have a zero instead of an O
The error always tells you where to look. In this case it says "near '0R..."
And sure enough, that's a zero, not an O. It should be the word OR.
Related
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 ?
I have a calculated field on a table at MS Access and I'd like to use the following syntax, but it returns a syntax error pop-up and and I'm wondering which part is wrong. Could you please help with this? Thanks a lot.
IIf([Letter_Grade]="A+",4,IIf([Letter_Grade]="A",4,IIf([Letter_Grade]="A",3.7,IIf([Letter_Grade]="B+",3.3,IIf([Letter_Grade]="B",3,IIf([Letter_Grade]="B",2.7,IIf([Letter_Grade]="C+",2.3,IIf([Letter_Grade]="C",2,IIf([Letter_Grade]="C",1.7,IIf([Letter_Grade]="D+",1.3,IIf([Letter_Grade]="D",1,IIf([Letter_Grade]="D-",0.7,IIf([Letter_Grade]="F",0)))))))))))))
Try adding ",null" after your last expression:
IIf([Letter_Grade]="F",0,null)))))))))))))
Though it may be better to use a query?
The function works well, did you put = operator before it.
Attached are some images of the results I got and the function
Here is the link to my sql code
https://www.mycompiler.io/view/5Lecen1
I don't know why but this is giving error when I am running it in an online sql compiler. It is giving a syntax error but when I am removing the operator "some" it is working fine. This is happening only when I use "some or any" keywords in subqueries can someone please help me find out what am I doing wrong
Syntax for SOME specifies the equality operator as '=', not '==':
https://dev.mysql.com/doc/refman/8.0/en/any-in-some-subqueries.html
Hello Team I new to ACCESS, getting a syntax error for the below query
I think your problem is all the double quotation marks. Try this:
SELECT HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO, HRMNZ_TRF_SCH.SCH_B_CMDTY_CD, CAT_ID_HTSUSA_XREF.CAT_ID_NO_20, CAT_ID_HTSUSA_XREF.LAST_UPDT_TS, CAT_ID_HTSUSA_XREF.LAST_UPDT_LOGON_ID
FROM Z1TJ111$.HRMNZ_TRF_SCH HRMNZ_TRF_SCH INNER JOIN Z1TJ111$.CAT_ID_HTSUSA_XREF.CAT_ID_HTSUSA_XREF ON HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO=CAT_ID_HTSUSA_XREF.HRMNZ_TRF_SCH_NO
WHERE NOT (HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='7211.23.3000' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8427.20.8000' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8427.90.0000' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8525.50.7010' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8525.60.1050' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8526.10.0040' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8526.91.0020' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8526.91.0040' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='8527.99.1500' OR HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO='9014.10.7080') AND HRMNZ_TRF_SCH.HRMNZ_TRF_SCH_NO<>HRMNZ_TRF_SCH.SCH_B_CMDTY_CD
Use the query editor to get tips about what is wrong. If you don't know how here is a tutorial: https://www.tutorialspoint.com/ms_access/ms_access_sql_view.htm
I have the following 2 mysql select queries within a PHP if statement:
if ($chooselocn =="") {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
else {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
outcodepostcodes.lat <= $latpoint
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
The first query works but the second returns this error message:
Query failed: 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 outcodepostcodes.lat <= 51.491239000000000 AND (hide='0' OR hide IS NULL OR' at line 8
Even if I remove the:
AND
outcodepostcodes.lat <= $latpoint
from the 2nd query making the two identical I still get similar error msg in the second.
Any ideas greatly appreciated.
You probably have an empty $where_no_and variable, thus your second query gets to contain ... WHERE AND ... which is not valid SQL
I bet the variable $where_no_and causes the issue.
It seems like it is empty causing the WHERE statement to be followed by the AND.
So this line:
WHERE $where_no_and AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
is like this:
WHERE AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
The WHERE AND is not valid syntax.
Even after you removed the part:
AND outcodepostcodes.lat <= $latpoint
Your line would look like this:
WHERE $where_no_and AND (hide='0'...
Which would result in this:
WHERE AND (hide='0'...
Again you have WHERE AND which is not valid syntax.
Try to make sure that the $where_no_and variable is not null or empty.
If your business logic says that this variable COULD be empty then you have to write a couple of more lines of code to handle this case.
Cheers!
This is not a solution but a way to find the bug.
Just put a var_dump($query) after the if(). It's impossible to tell without knowing what the variables actually contain. The dump will be before the query is executed, so you'll see very well what is going on there.
If the query is still apparently correct post the dump here and we'll see.
Why do if/then if the queries are supposed to be identical regardless of the condition?
That said, your variables that form the query are probably different depending on the value of $chooselocn. I would start by dumping the actual query into debugging log or the browser as well as the variables that compose it and it would shed some light.
I should also add the standard warning about watching out for SQL injections.
Thank you, thank you, thank you and a MASSIVE THANK YOU to every one who replied to my question!!!!
#Gordon Linoff, thank you for being the first to respond in the right direction
#David Lavieri, thank you for the useful suggestion
#Tudor Constantin, thank you for being the first to explain the crux of the problem
#pid, thank you for the useful suggestion
#user2399432, a massive thank you for the very lengthy, detailed and exhaustive explanation of EXACTLY what was going on.
While everyone pointed me in the right direction, and unfortunately I was busy elsewhere to follow up on those early suggestions, I must upvote #user2399432 for going to all that trouble to make absolutely sure that it all sunk in and there were no two ways about it.
Just as background info, this is an extremely old site I have come back to work on, after a three year absence from coding, and I noticed that I had the following lines of code in this particular script:
///THIS IS THE CRUCIAL LINE BELOW:
$where_no_and = rtrim($where, 'AND ');
///End of crucial line and then TEST
//var_dump($where_no_and);//VERY USEFUL DIAGNOSTIC! INDISPENSIBLE! MUST RETAIN! DO NOT DELETE!
//echo "#6 City is:".$lc_city;//USEFUL DIAGNOSTIC
So I must have had the same problem those many years ago and dealt with it accordingly using var_dump($where_no_and);
Be that as it may, I was well and truly stuck this time round and your valuable help has knocked down the barricades, SO THANK YOU TO ALL!