Mysql 5.5 IF statement [closed] - mysql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Using Mysql 5.5. Trying to use IF statement:
mysql> SELECT description IF(1=1,'ok','no') FROM my_table;
ERROR 1064 (42000): 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 'IF(1=1,'ok','no') FROM my_table at line 1
Ideas?

Correct syntax of IF() in mysql query is:
SELECT IF(condition, 'Some Result If True', 'Some Result If False'), OTHER_COLUMNS
FROM ...
WHERE ...
so you should use
SELECT description,IF(1=1,'ok','no') FROM my_table;

As a technical note, this is the if() function, not the if statement. In any case, you are just missing a comma:
SELECT description, IF(1=1,'ok','no')
------------------^
FROM my_table;

Assumming you want description ='yes' for 1=1 otherwise no
SELECT
CASE WHEN 1=1 THEN 'ok' ELSE 'no' END as description

Related

How to use IN & NOT IN same query [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a query which consists of both IN & NOT in the query but it gives me an error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IN ('aniket27') ORDER BY id DESC LIMIT 2' at line 1
This is how my query looks
SELECT * FROM users
WHERE username IN ('aniket27','deepakrajak','bhawana','Mehul13','sanchayeeta',
'shivajidutta', 'anamika_4a','parekh','anupamkumar')
AND NOT IN ('aniket27')
ORDER BY id DESC LIMIT 2
You must specify column for NOT IN
SELECT * FROM users WHERE username IN ('...') AND username NOT IN ('aniket27') ORDER BY id DESC LIMIT 2
But first condition will be sufficient to match elements that are listed in NOT IN part, just remove them from first IN list.
You forgot to put the field name before "NOT IN" :
SELECT * FROM users WHERE username IN ('aniket27','deepakrajak','bhawana','Mehul13','sanchayeeta','shivajidutta','anamika_4a','parekh','anupamkumar') AND username NOT IN ('aniket27') ORDER BY id DESC LIMIT 2
try this:
SELECT * FROM users
WHERE username IN ('aniket27','deepakrajak','bhawana','Mehul13','sanchayeeta','shivajidutta','anamika_4a','parekh','anupamkumar')
AND username NOT IN ('aniket27')
ORDER BY id DESC LIMIT 2;

getting error while copy data from one table to another table, sqlite [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have two table named
state_master
location_master_state_master
Hi I want to copy data from one table to another but I am getting following error
SQLiteManager: Likely SQL syntax error: INSERT INTO state_master(id, create_uid, code, create _date, name, write_uid, write_date) SELECT id, create_uid, code, create_date, name, write_uid, write_date FROM location_master_state_master; [ near "create": syntax error ]
Exception Name: NS_ERROR_FAILURE
while executing below query
INSERT INTO state_master(id, create_uid, code, create _date, name, write_uid, write_date) SELECT id, create_uid, code, create_date, name, write_uid, write_date FROM location_master_state_master;
would anybody help me how to resolve this error?
There is a space between create and _date which should be create_date:
INSERT INTO state_master(id, create_uid, code, create_date, name, write_uid, write_date) SELECT id, create_uid, code, create_date, name, write_uid, write_date FROM location_master_state_master;

replacing column values with Y and N [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a table which contains column Flag with values(Y,N) ,when I write select Query it should return me result as (Yes,No) .how to achieve that????Please Help
Try this:
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TableName
Syntax:
CASE column_name
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
Read more about CASE here.
Try this
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TABLENAME
You can also try if condition apart from casing as suggested above:
SELECT IF(flag='Y','Yes','No') FROM mytable;

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 'IN [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I wonder why my query isn't working, I am supposed to leave the WHERE 1 just as it is; I've used it like this many times before but for this query, it is not working.
SELECT * FROM industries WHERE 1 AND IN id (11,8,1,2,3,4,5,6) ORDER BY position ASC
Here is the error I get:
1064 - 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 'IN id (11,8,1,2,3,4,5,6) ORDER BY position ASC LIMIT 0, 30' at line 1
SELECT * FROM industries WHERE id IN (11,8,1,2,3,4,5,6) ORDER BY position
Don't use WHERE 1 it does not make sense here. It is useless because it is evaluated as (always) true
And the correct syntax for IN is WHERE <column> IN (list)
SELECT * FROM industries WHERE 1 AND id IN (11,8,1,2,3,4,5,6) order by position;
This query should work:
SELECT * FROM industries WHERE id IN (11,8,1,2,3,4,5,6) ORDER BY position ASC
ASC is default, you can write just
ORDER BY position

Select order by... cant' find error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a problem, trying to sort my Select request -
SELECT * FROM `table_name` OREDER BY `score` DESC LIMIT 10 ;
I get error #1064:
#1064 - 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 'BY score DESC LIMIT 10' at line 1
But I realy don't know wats wrong ! Names and quotes are right, as I think... as this request works correctly:
INSERT INTO `table_name` (`uid`, `score`) VALUES ("'.$viewer_id.'","'.$uscore.'") ON DUPLICATE KEY UPDATE `score` = "'.$uscore.'";';
Can somebody help me!!??
P.S sorry for my English(
it's ORDER not OREDER
SELECT * FROM table_name ORDER BY score DESC LIMIT 10
As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.
How to prevent SQL injection in PHP?
You have a spelling error in your syntax it's 'ORDER BY' not 'OREDER BY' the correct syntax should be:
SELECT * FROM `table_name` ORDER BY `score` DESC LIMIT 10 ;