Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAVING LENGTH(field) in MySQL

Tags:

sql

mysql

I'm trying to select rows with field timestamp, which has a length shorter than 16 characters. I've tried the following:

SELECT LENGTH(timestamp), id
FROM my_table
HAVING LENGTH(timestamp) < 16

But I get this error:

#1054 - Unknown column 'timestamp' in 'having clause'

Any suggestions?

like image 524
user1235446 Avatar asked Dec 29 '25 20:12

user1235446


1 Answers

I think you want:

SELECT LENGTH(`timestamp`), id
FROM my_table
WHERE LENGTH(`timestamp`) < 16

Or if you're actually trying to group the results...

SELECT LENGTH(`timestamp`)
FROM my_table
GROUP BY LENGTH(`timestamp`)
HAVING LENGTH(`timestamp`) < 16

Note the backticks (`) in each example to escape the column name.

like image 177
Yuck Avatar answered Jan 01 '26 11:01

Yuck