-1
tableName

|name    | header | date      |
|--------|--------|-----------|
|abc     | def    | 2016-08-16|
|pqr     | xyz    | 2015-11-20|

I wanted to write a query to display the name which was entered the earliest.

select name from tableName where date(date) = min(date) encountered an error.

I searched the internet for answers but couldn't find what I was looking for. Any advice and help on this matter would be welcome.

Nishanth Rao
  • 11
  • 1
  • 1
  • 4
  • See: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Sep 15 '17 at 16:30
  • Answer here is applicable to your question (swap max with min) https://stackoverflow.com/questions/19432913/select-info-from-table-where-row-has-max-date/19433107#19433107 Might be overkill, baao's answer is good. – Twelfth Sep 15 '17 at 17:48

2 Answers2

3

Use order by and limit

SELECT `name` 
FROM tableName 
ORDER BY `date`
LIMIT 1

Note that name as well as date are mysql keywords, so you shouldn't use them as a column name.

baao
  • 71,625
  • 17
  • 143
  • 203
  • They may be keywords, but they're not reserved, so their use as column names is fine in my book. – Strawberry Sep 15 '17 at 16:33
  • Yeah, that's why I wrote **shouldn't**. @Strawberry . It's no error for sure, but I try to avoid using keywords as it's only confusing... select date(date) sounds strange to me. – baao Sep 15 '17 at 16:35
  • 1
    Well sure, because in that context `date` would be a datetime ;-) – Strawberry Sep 15 '17 at 16:36
0

You can use

select name from tableName where date(date) = (select min(date) from tableName)
Pramod Yadav
  • 245
  • 2
  • 20