Unable to get mysql to recognise query using in statement - mysql

I've been attempting to adjust a mysql SELECT statement based on input from checkboxes. The php code collects the ticked checkboxes into an array, implodes them into a comma-separated list and then runs the query using an in statement (as was detailed here).
The query generated comes out as SELECT * FROM events WHERE Discipline IN (SJ,OTHER) which is a correctly formatted query as far as I can tell.
This shows up as an invalid query when run from the php code. When I run the query using phpmyadmin, I receive this message:
#1054 - Unknown column 'SJ' in 'where clause'
I was wondering if anyone could tell my why that query is generating the error?

Try putting quotes around the values in the IN clause:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')

If SJ and OTHER are string literal use:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')

Related

SQL Query Differences between MS Access and MySQL

I'm having trouble converting a MS Access Query into a MySQL view. It seems that in Access, you're able to use a field that's created within the same select statement later on in the select statement. Whereas, in MySQL, that seems to not be the case.
e.g. Access Generated Code
SELECT IIf([Bonus]>0,[Base Salary],0) AS [BE Base], [BE Base]+[Bonus] AS [BE TC]
FROM titles_tbl INNER JOIN wage_tbl ON titles_tbl.Job_Title = wage_tbl.[Standard Title]
WHERE (((titles_tbl.Mod_Code)="3B") AND ((wage_tbl.Bonus)>0));
vs MySQL Converted Code
select if(`bonus`>0,`base salary`,0) as `be base`, `be base`+`bonus` as `be tc`
from titles_tbl inner join wage_tbl on titles_tbl.job_title = wage_tbl.`standard title`
where (((titles_tbl.mod_code)="3b") and ((wage_tbl.bonus)>0));
When I put this Access-generated SQL code into MySQL (after converting it appropriately), I get an error saying
10:17:07 Error Code: 1054. Unknown column 'be base' in 'field list' 0.062 sec
To me, it seems like MySQL can't use a new column in the same select statement that it's generated in.
Does anyone know whether that's the case, or what the real problem might be (as well as a solution)? Thank you in advance!
Your MySQL code should be
select if(`bonus`>0,`base salary`,0) as `be base`,
((SELECT `be base`) + `bonus`) as `be tc`
from ...
Firstly, if you use spaces in aliases you should surround the alias with ' (quote) or ` (backticks)
Secondly, if you want to refer, in a SELECT, to an alias of the same SELECT, you have to (SELECT `the_alias`)
And thirdly, in the subselect it is mandatory to wrap the aliased column with ` (backticks), simple quotes won't work there.

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. :)

How to use if condition in SOQL?

How to reproduce the following statement of MySQL in SOQL:
SELECT IF(1 = 2,'true','false');
I am trying to do the following:
select if(field1=null,false,true) as res from table
But it is showing me unknown parsing error.
That is not possible in SOQL.
Not sure of the requirement here but you may use a formula field instead.

nested "select " query in mysql

hi i am executing nested "select" query in mysql .
the query is
SELECT `btitle` FROM `backlog` WHERE `bid` in (SELECT `abacklog_id` FROM `asprint` WHERE `aid`=184 )
I am not getting expected answer by the above query. If I execute:
SELECT abacklog_id FROM asprint WHERE aid=184
separately
I will get abacklog_id as 42,43,44,45;
So if again I execute:
SELECT `btitle` FROM `backlog` WHERE `bid` in(42,43,44,45)
I will get btitle as scrum1 scrum2 scrum3 msoffice
But if I combine those queries I will get only scrum1 remaining 3 atitle will not get.
You Can Try As Like Following...
SELECT `age_backlog`.`ab_title` FROM `age_backlog` LEFT JOIN `age_sprint` ON `age_backlog`.`ab_id` = `age_sprint`.`as_backlog_id` WHERE `age_sprint`.`as_id` = 184
By using this query you will get result with loop . You will be able to get all result with same by place with comma separated by using IMPLODE function ..
May it will be helpful for you... If you get any error , Please inform me...
What you did is to store comma separated values in age_sprint.as_backlog_id, right?
Your query actually becomes
SELECT `ab_title` FROM `age_backlog` WHERE `ab_id` IN ('42,43,44,45')
Note the ' in the IN() function. You don't get separate numbers, you get one string.
Now, when you do
SELECT CAST('42,43,44,45' AS SIGNED)
which basically is the implicit cast MySQL does, the result is 42. That's why you just get scrum1 as result.
You can search for dozens of answers to this problem here on SO.
You should never ever store comma separated values in a database. It violates the first normal form. In most cases databases are in third normal form or BCNF or even higher. Lower normal forms are just used in some special cases to get the most performance, usually for reporting issues. Not for actually working with data. You want 1 row for every as_backlog_id.
Again, your primary goal should be to get a better database design, not to write some crazy functions to get each comma separated number out of the field.

Select from View using Where clause to refer to another database - Unknown Column error

I am trying to select certain values from a view that I created. The statement is below:
SELECT * FROM dashboard.team
WHERE ac2012.acx_users.id = 1;
As you can see, there are 2 databases being referenced here:
dashboard database, team table
ac2012 database, acx_users.id table
ac2012.acx_users.id is the regular expression in the original Create View statement, I'm using that since of course I can't use an ALIAS in a Where clause... however, this is showing an error:
Error Code 1054: Unknown column 'ac2012.acx_users.id' in 'where clause'
I'm not sure how to get this to work, because I need to reference the other database in this case, but it's not recognizing the database. Any tips would be appreciated.
Since you're selecting from a view, the underlying databases aren't visible anymore. You only see what the view presents, as part of the database which the view lives in, so try WHERE acx_users.id = 1, or whatever you've aliased that field to in the view definition.
SELECT * FROM dashboard.team
LEFT OUTER JOIN ac2012 ON ac2012.CommonColumnName=dashboard.CommonColumnName
WHERE ac2012.acx_users.id = 1;
======================
Please replace by original column name ...