How to use IN & NOT IN same query [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 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;

Related

Order by expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would I do the following:
select * from table order by istrue(field > 90)
What does istrue() do? I don't recognize it as a MySQL function.
Perhaps your intention is to order things so the values of field greater than 90 are first. If so:
order by (field > 90) desc
In a numeric context, MySQL treats boolean expressions as integers with "1" being true. Hence the desc to get the true values first.
use Case statement;
select * from table
order by
case when field > 90 then ordercolumn end
Maybe what your trying to achieve is this:
SELECT * FROM table WHERE field > 90 ORDER BY field
I think that this is what you want:
SELECT * FROM table ORDER BY (field > 90) DESC
Or maybe:
SELECT * FROM table WHERE field > 90 ORDER BY field DESC

MySQL limit clause not accepting 2 arguments [duplicate]

This question already has an answer here:
Mysql delete statement with limit
(1 answer)
Closed 8 years ago.
I have really searched this and find nothing about this problem.
Very simple query:
/*get rid of 125 rows starting at 100*/
delete from my_table WHERE category=5 order by editdate DESC LIMIT 100, 125;
This is what is returned:
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 ' 125' at line 1
I have not used LIMIT in a while, but at the same time I have not had a problem with it. Error is very mysterious, any ideas?
My version of mysql is 5.x
This is because the MySQL syntax for DELETE, doesn't allow a second parameter for LIMIT like in the SELECT case. Please check the manual.
You can't use a LIMIT offset on a DELETE command. Here's a way to do it:
delete from my_table WHERE ID IN (SELECT ID from my_table WHERE category=5 order by editdate DESC LIMIT 100, 150);
Where ID is the primary ID for my_table.
delete from my_table WHERE category=5 order by editdate DESC LIMIT 125;
This will work but you can't use limit like 100,125 . because it shows the selection between rows.

Mysql 5.5 IF statement [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 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

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 ;