Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Is there a way to use sql functions like min as values in where clause of mysql?

i am having problem with the mysql statements... the answer is to get the name of city and length of city of min length from a table named station where ordered by alphabetically by city names i have tried some statements as described below.

SELECT City, LENGTH(City) FROM Station ORDER BY City WHERE LENGTH(City) = MIN(LENGTH(City));

I have also tried to use user defined varibales like below:

SELECT @min := MIN(LENGTH(City)) FROM Station ORDER BY City;

SELECT City, LENGTH(City) FROM Station ORDER BY City WHERE LENGTH(City) = @min;

Sample Output: Amo 3

it showing this error : check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE LENGTH(City) = @min'

But it's not working please help

question from:https://stackoverflow.com/questions/65868423/is-there-a-way-to-use-sql-functions-like-min-as-values-in-where-clause-of-mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have got this solution. I can use complex sql statements to equalize the length of the city to the minimun length by using the sql statements below:

SELECT City, LENGTH(City) FROM Station WHERE LENGTH(City) = (SELECT MIN(LENGTH(City)) FROM Station) ORDER BY City LIMIT 1;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...