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

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 ?

Related

Getting SQL Error: "SELECT" is not valid at this position for this server version, expecting '(' with

I have been trying out the following query in MySQL Workbench:
SELECT NAME,LEAD,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,LEAD,OUTCOME ASC;
However I am getting this error:
"SELECT" is not valid at this position for this server version, expecting '(' with
I have tried to find out where the error is coming from by taking away parts of the query and seeing where it breaks, it seems to work when I try:
SELECT NAME
FROM OUTCOMES_BY_USER;
However when I add in another column (as shown below) I start getting the same error:
SELECT NAME, LEAD
FROM OUTCOMES_BY_USER;
I am really not sure how to get around this error, I was trying this query in sqlfiddle and it worked fine, however my sqlfiddle suddenly stopped working and the website just flat out wont build schemas for me anymore. so I tried it out on a my universities MySQL server and have been getting this error. Please help!
LEAD() is a MySQL function, hence it is a reserved word. You can add backticks around reserved table or column names to bypass this error :
SELECT NAME,`LEAD`,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,`LEAD`,OUTCOME ASC;

MySQL error 1064 - what is causing it?

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.

cfm websql queries error

I have this websql script (http://pastebin.com/gvJseBAn) which doesn't perform correctly.
If I run the statement select * from news where id=0772348890 , I get the error The conversion of the varchar value ' 0017707787068' overflowed an int column.
If I run the statement select * from news where id='0772348890' , I get the error Incorrect syntax near '0772348890'.
If I run the statement select * from news where id="0772348890" , I get Invalid column name '0772348890'
Any other variation of '#0772348890#' or #0772348890# or "#0772348890#" I have tried gives the error "incorrect column" or "incorrect syntax near ..."
Any ideas on how to fix this error, or a better method of creating a simple websql query form?
A) the issue here is that db column will not under any conditions accept "0772348890" as a valid input because it is mismatched. The column is an "int" type (according to your first error), but your value has a padded 0 prependedto the front as in 0 772...
What is the purpose of this zero? Ordinarily prepended zeros appear in fixed length character fields where a space is not allowed. Should the value not be "772348890"?
B) Remember that ColdFusion will escape your single quotes in your query. In your second error example (where you use single quotes), this code:
<cfquery name="runsql" datasource="#Form.datasource#" timeout="30">
#Form.sql#
</cfquery>
Produces this SQL statement:
select * from news where id=''0772348890''
Which would give you your syntax error. If you wish to successfully test your second example you will need to alter your code to:
<cfquery name="runsql" datasource="#Form.datasource#" timeout="30">
#preservesinglequotes(Form.sql)#
</cfquery>
Preservesinglequotes() gets you past the second error issue and MSSQL's implicit conversion may strip off the prepended zero and allow the query to succeed - though I'm not sure will give you what you want.
C) Finally you should probably never do what you are trying to do - at least not in this fashion (sorry to be so direct!). Your opening up your DB to arbitrary queries from a web form. The resulting damage from even casual mistakes could be catastrophic to your data, let alone a malicious user bent on stealing or altering or using your site for malicious purposes. That's my take. :)

Excel to SQL - CEILING and Error code 1582

I have the following excel formula:
CEILING(F9*6763.85873627538/((F9-1)*400+6763.85873627538),1)
Where F9 is named PROJECTED_QUANTITY in my table sample_size_by_service_id
I have the following SQL query written out:
select
PROJECTED_QUANTITY, ceiling((PROJECTED_QUANTITY*6763.858736275380)/((PROJECTED_QUANTITY - 1)*400+6763.85873627538),1)
FROM sample_size_by_service_id
and I'm getting the following error message:
Error Code: 1582. Incorrect parameter count in the call to native
function 'ceiling' 0.047 sec
Not sure what I'm doing wrong here - I'm a novice to SQL and my hours of googling have had me make sure that my PROJECTED_QUANTITY column is an INTEGER type (it is). I suspected that I am badly badgering the combined operators, but I'm not sure how else to write it.
Appreciate any insight! Thanks!
At the suggestion of user3964075, I removed the second parameter as the mysql syntax for CEILING is CEILING(number), not CEILING(number, significance) as it is in excel. Removing the second parameter resolved the issue - I am no longer getting error code 1582 and am achieving the desired results.

MySQL Query Error Validation

I running a Mysql Query to select some data, Sometimes i get a error called
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
when i executed this following code,
$result = $this->db->execute($sql);
for ($i = 0; $data[$i + 1] = mysql_fetch_assoc($result); $i++);
array_pop($data);
how do i optimize this coding to prevent any errors ?
is there anything wrong with it ? should i ignore this error ?
That means that the query is buggy, whyever, most likely because you construct it using components from sources which you do not really check enough. A buggy statement throws an error (since no result can be computed). That error is returned as false instead of a mysql result ressource. Since you do not check if the query succeeded but blindly try to retrieve details from the result, you get this second error.
So there are four things you have to invest into:
you should always check if a query succeeded at all:
enclose your query into a conditional: if (FALSE!==($result=$this->db->execute($sql))) and only retrieve from the result ressource if that condition resolves to true.
make sure you really (really!) check all input data you use to construct your query. Checking here also means to encode and escape it correctly, also see point 4. for this.
in cases like this it is important to analyze what exactly it is that is going wrong. There is little sense in guessing what might be going wrong. So in addition to checking if the query succeeded at all (1.) you should also take a look at the error message mysql throws if this is not the case. Use the method mysql_error() for this. It is well documented just as every other function too.
you should rework your code and migrate from phps old, long deprecated mysql extension to either mysqli or PDO. Both are php extensions that offer more security against constructing buggy statements. Read about "prepared statements" and "parameter binding" for this.