I am stumped with a mysql query. I am attempting to bring back all unique values from two columns sorted by date. Here is my table:
from to body created_on
+55555555555 +22222222222 hello 2015-03-20 01:00:00
+11111111111 +33333333333 hello 2015-03-21 01:00:00
+33333333333 +44444444444 hello 2015-03-18 01:00:00
+66666666666 +11111111111 hello 2015-03-12 01:00:00
+11111111111 +77777777777 hello 2015-03-05 01:00:00
Basically it is a table that stores sms messages and I want to pull back the 100 most recent phone numbers that have entries into the sms table. So something like this:
number
+11111111111
+77777777777
+66666666666
+22222222222
+33333333333
Another requirement is that I don't pull bring back the phone numbers +4444444444 and +555555555555 because they are administrator numbers. The numbers should also be sorted by date.
Here is what I have so far:
SELECT `from` as num, created_at FROM sms WHERE `from` NOT IN (+4444444444, +5555555555)
GROUP BY num
UNION
SELECT `to` as num, created_at FROM sms WHERE `to` NOT IN (+4444444444, +5555555555)
GROUP BY num
ORDER BY created_at
I know I'm close but it's bringing back duplicates which I don't want.