Ashish Roy
Ashish Roy

Reputation: 3

AND condition not working in mysql

I am building an application. i need to fetch and display items from the database table. I am trying to select the items from table where the Code is LIKE ZW120 AND Checkedout is NOT equal to 1.

So i am using this SQL command -

SELECT * 
FROM inventory_table 
WHERE Code LIKE'%ZW120%' AND Checkedout!='1'

This SQL command should show 0 results, as there are only two items in the table with code ZW120, and both of these items have Checkedout column set to 1.

The problem is that it keeps showing two results. No result should be returned by mysql.

Here is a screenshot to explain my issue better.

Thanks in advance.

Upvotes: 0

Views: 227

Answers (3)

MiKE
MiKE

Reputation: 524

I have just made an exact copy of what you are describing there. I copied also your SQL statement and the results are just they way they should be (The way you explain).

Check the sqlfiddle for example and run the query.

Link http://sqlfiddle.com/#!2/9e2cb/1

AS you can see, i've made 2 columns (well, 3, the first one is autoincrement) and added 2 rows in it. Both having the matching "ZW120" code and the checkedout to 1. When you run the SQL statement, the results are 0. That is correct.

You can change one row value to "0" and see that it finds 1 row that matches the SQL statement, which is also correct!

I'm a but confused now. Could you tell us what database you are using and which tool to run the SQL? I know this might not be helpful but as it is now i have NO clue what is wrong.

Kind Regards, MiKE

Upvotes: 1

Victor
Victor

Reputation: 8480

Not Equal is <> not != in SQL

Use:

SELECT * FROM inventory_table WHERE Code LIKE'%ZW120%' AND Checkedout <> 1

Upvotes: 0

Etixpp
Etixpp

Reputation: 328

I guess Checkedout is a numeric column so you have to delete the '' arround the 1.

Try

SELECT * FROM inventory_table WHERE Code LIKE'%ZW120%' AND Checkedout <> 1

Upvotes: 0

Related Questions