I am trying to convert my MS Access database to MYSql.
I have some queries where I create a column based on the results of an IF statement.
In Access the query goes something like this:
SELECT table.column_A, table.column_B, IF(table.column_B = "1", 0, IF(table.column_B = "2", [table.column_C1], IF(table.column_B = "3", [table.column_C1]+[table.column_C2],0))) AS column_D
FROM table
How do a write this for MySql?
6 1770
The IF statement is fine for MySql.
Remove the square brackets or replace them with backticks
Here is a query that I tried in MYSQL that doesn't work. It says that there is an error in my SQL syntax near the IF statement.
What is wrong with it?
SELECT
`1stcalldivided`.TN,
`1stcalldivided`.RD,
`1stcalldivided`.RN,
`1stcalldivided`.HNa,
`1stcalldivided`.1ston,
IF (`1stcalldivided`.1ston = "1", "3", "2") AS final
FROM
`1stcalldivided`
I really can't see anything wrong.
But technically this is an IF function not an IF statement. The two differ
The only possibilities I can think of are:
The IF function is not supported in older MySql versions.
You have a missing comma or backtick or one too many.
The follwing nested IF works fine for me - SELECT `product_id`, `code`,IF(`stock_level`<1,
-
IF(`products`.`lead_time_id`=1,'YES',`lead_time`),'YES')
-
AS available
I am using MYSQl 5.0.
I did some reading and I think that I have to put the IF function in a stored procedure or a function. What version of MYSql can use the IF function right in the query the way you are doing it, and the way I have been doing it in MS Access?
I do not have the patience or desire to figure out how to make the stored procedure in MYSql 5.0 work.
It is a Control Flow function and doesn't need to be in a stored procedure.
What about trying the CASE statement instead? - SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
-
'
will return 'true
Thanks for the encouragement. I got the if statement to work in my query.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fábio Mendes |
last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with
things or the python-list sent my msg to /dev/null. I couldn't find
anything related in previous PEP's, so here it goes a...
|
by: Nerox |
last post by:
Hi, If i write:
#include <stdio.h>
int foo(int);
int main(void){
int a = 3;
foo(a);
}
|
by: eman1000 |
last post by:
I was recently looking at the prototype library
(http://prototype.conio.net/) and I noticed the author used the
following syntax:
Object.extend(MyObj.prototype, {
my_meth1: function(){},...
|
by: Steven Bethard |
last post by:
The PEP below should be mostly self explanatory. I'll try to keep the
most updated versions available at:
http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
|
by: Steven Bethard |
last post by:
I've updated the PEP based on a number of comments on comp.lang.python.
The most updated versions are still at:
http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
|
by: Steven Bethard |
last post by:
Ok, I finally have a PEP number. Here's the most updated version of the
"make" statement PEP. I'll be posting it shortly to python-dev.
Thanks again for the previous discussion and suggestions!...
|
by: Steven Bethard |
last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The
patch is available at:
http://bugs.python.org/1472459
and I've pasted the full text below.
I've tried to be more explicit about...
|
by: Steve |
last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement
I got ASP error number 13 when I use the SELECT...FOR UPDATE statement
as below.
However, if I use SELECT statement without...
|
by: dspfun |
last post by:
Hi!
The words "expression" and "statement" are often used in C99 and C-
textbooks, however, I am not sure of the clear defintion of these
words with respect to C.
Can somebody provide a sharp...
|
by: florian.loitsch |
last post by:
According to the spec Section 14 the production
SourceElements:SourceElements SourceElement is evaluated as follows:
1. Evaluate SourceElements.
2. If Result(1) is an abrupt completion, return...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |