Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "SELECT ... WHERE id=1=0" returns all rows except one with id=1?

Tags:

sql

mysql

Why the following query:

SELECT * FROM myTable WHERE id=1=0

returns all rows from myTable except one which has id=1?

myTable content:

+----+-------+
| id | value |
+----+-------+
| 1  | dog   |
| 2  | cat   |
| 3  | parrot|
+----+-------+

Now run: SELECT * FROM myTable WHERE id=1=0

Output:

+----+-------+
| id | value |
+----+-------+
| 2  | cat   |
| 3  | parrot|
+----+-------+
like image 429
kozooh Avatar asked Jan 23 '19 12:01

kozooh


People also ask

How do I SELECT all rows except one?

You have a few options: SELECT * FROM table WHERE id != 4; SELECT * FROM table WHERE NOT id = 4; SELECT * FROM table WHERE id <> 4; Also, considering perhaps sometime in the future you may want to add/remove id's to this list, perhaps another table listing id's which you don't want selectable would be a good idea.

Why is SQL only returning 1 row?

To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement. The LIMIT clause is used to control the number of rows returned by your query. When you add LIMIT 1 to the SELECT statement, then only one row will be returned.

What is the difference between SELECT * and SELECT 1?

Select * from any table will fetch and display all the column in that table, while Select 1 from any table will display one row with 1 without any column name.

How to select all records except a row with certain ID?

How to select all the records except a row with certain id from a MySQL table? To avoid displaying a certain id from a table, you need to use the <> operator, which is the NOT EQUAL operator. Let us first create a table −

How to select all rows from a table except the last one?

How to select all rows from a table except the last one in MySQL? You need to use != operator along with subquery. The syntax is as follows − To understand the above syntax, let us create a table.

How to hide all rows except selected one without ID using JavaScript?

Given an HTML table with a set number of rows, the task is to hide all the rows except the selected one without knowing its ID using JavaScript. Approach: Using the querySelectorAll (), forEach (), addEventListener () and filter () methods & the spread operator (…) in native JavaScript.

How to remove parent and child rows when selected rows exist?

1 Create rows when selected rows exist 2 Remove parent and child rows if child or parent fits criteria 0 Check for existing matches to find the field their grouped-by 6 T-SQL MINUS operator 7


2 Answers

The reason is that the logic should be being evaluated as:

WHERE (id = 1) = 0

This is equivalent to:

WHERE (id = 1) "is false"

Or:

WHERE id <> 1

Try running these examples:

select 1=1=0, 1=2=3, 1=1=0
like image 168
Gordon Linoff Avatar answered Nov 15 '22 21:11

Gordon Linoff


Default operator precedence works as follows :

WHERE (ID=1)=0

Which resutls false in case of id=1

like image 33
Muhammad Waheed Avatar answered Nov 15 '22 20:11

Muhammad Waheed