php


Compare latest and earliest dates in mysql table and display changes only


I have a table called archive:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
I want to choose latest date and earliest date then compare this two queries and display only those rows which have been changed. So from table above it should display:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
As only this row was changed.
I know how to choose earliest date in queries:
SELECT staff_id, longname, username, title, process, creation_time FROM archived
WHERE creation_time IN (SELECT min(creation_time) FROM archived)
And latest date:
SELECT staff_id, longname, username, title, process, creation_time FROM archived
WHERE creation_time IN (SELECT max(creation_time) FROM archived)
The question is how can i compare queries and display only changes?
EDIT
Ok so what i have is table archive:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
Table updates every day on 00:00:00. So latest day will be different. What I want is to take rows with first date:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
And with latest date:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
Then compare row by row. For example:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
with
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
Rows are the same so I dont need to display them as
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
with
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
These rows also same. So the only row I want to print is
| staff_id | longname | username | projectNo | title | process | creation_time |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
becaude its different.
Try this:
select tmax.*
from yourtable tmax
inner join (
select staff_id, max(`creation_time`) as maxtime, min(`creation_time`) as mintime
from yourtable
group by staff_id
) t on tmax.staff_id = t.staff_id and tmax.creation_time = t.maxtime
inner join yourtable tmin
on t.staff_id = tmin.staff_id and tmin.creation_time = t.mintime
where tmax.projectNo <> tmin.projectNo or tmax.title <> tmin.title or tmax.`process` <> tmin.`process`
See sqlfiddle demo here.
Subquery will fetch max time and min time for each staff_id, then join your table twice with staff_id and max, min time, in where statement is your criteria.

Related Links

PHP - upload a pdf file with separating each page to mysql database
SAPUI5 For the UI and LARAVEL for backoffice
How to apply lowercase filter to GridView?
Wordpress inserting rewrite_rules option more than once
Yii2: how to use component in ActiveController default action
replace a string inside a specific tag
How to send an email with php code?
how to make an api for ionic from two tables(users and posts) in database
How to create/update bulk of emails to Mailchimp list using their api 3.0?
PHP - Large POST Request Server Error
Blowfish encryption - hash is created but will not verify
Access Value of an array in php inside of another Array
AWS s3 multipart upload fails + memory allocation exhausted + php
PHP how to save multiple upload image names and paths in different columns?
Unknown column 'Array' in 'where clause' SQL=SELECT * FROM #__content WHERE Array = '4'
Why showing undefined service and aliases in app file in laravel?

Categories

HOME
amazon-web-services
entity-framework
azure-functions
dotnetnuke
ews
website
docker-cloud
twitter-fabric
mathematical-optimization
dependencies
qooxdoo
biztalk-2010
ontology
position
eddystone
solution
colors
documentum
pywinauto
rest-assured
user-defined-types
significance
jitsi
liferay-6.2
qa
eclipse-emf
opentext
raima
unrar
spreadsheetgear
onchange
php-5.3
laravel-4.2
vcenter
advanced-filter
express-session
love2d
mv
resampling
icepdf
fiware-wirecloud
daz3d
rightnow-crm
viewmodel
cloudwatch
django-static-precompiler
lightning-workbench
ilrepack
android-webservice
palindrome
instafeedjs
bpms
pbx
allegro
bigdecimal
nsrunloop
p6spy
seccomp
xcode8-beta4
wicked-pdf
rails-engines
urbit
mathnet
fdt
accessibilityservice
wif4.5
gdl
boost-proto
nette
dsa
bjam
gitlab-omnibus
unidata
abstract-factory
mxe
incron
mutators
password-recovery
rebar
image-rotation
global-scope
multivariate-testing
osascript
box2dweb
accessory
jack
usb-flash-drive
nosql-aggregation
ctp
pbkdf2
mousemotionlistener
stty
libxml-js
oscilloscope
ofstream
significant-digits
onconfigurationchanged
server-name
canonicalization
ios-4.2
multiple-users
tessellation
subtext
levels

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App