Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query with condition when one field not equal to another

Tags:

sql

mysql

Let's say we have fields: id | content | updateTime | creationTime

When insertion done both updateTime and creationTime get filled with current time. On update only updateTime field get changed. When I need to find all data created on '2012-06-28' I simply use:

SELECT id, content FROM tbl WHERE creationTime LIKE '2012-06-28%'`

But to get all data that was updated excluding created data I need to use something like this:

SELECT id, content FROM tbl WHERE creationTime LIKE '2012-06-28%' AND creationTime != updateTime

This can't work, obviously. Though I could find all updated data by comparing values of two 'time' collumns inside php, I'd still love to do it inside query.

UPD: Well, it turns out that I was absolutely unaware that sql allows not only compare value of a field with a given variable, number... but also it can compare value of one field with another.

like image 780
Vitaly Lebedev Avatar asked Oct 16 '25 16:10

Vitaly Lebedev


1 Answers

Use <> instead != (that isn't compatible with some DMBS)

So

SELECT id, content 
FROM tbl 
WHERE creationTime LIKE '2012-06-28%' AND creationTime <> updateTime

If you want some documentation you can find here

However, your query have to work properly with !=

like image 158
DonCallisto Avatar answered Oct 18 '25 06:10

DonCallisto