Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Mysql if statement in select between two data

I have the following data in my query :

Year | Entitle  | 
2020 | 4        | 
2021 | 2        | 

How to do the conditional query if 'Entitle' is greater than other year then it will appear in new column. For example :

Year | Entitle  | Higher 
2020 | 4        | Yes
2021 | 2        | No
question from:https://stackoverflow.com/questions/65661727/mysql-if-statement-in-select-between-two-data

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

1 Answer

0 votes
by (71.8m points)

I would probably use variables but then the order will different since than you will be comparing against the previous row.

set @olden:=NULL;
select year,entitle,
  if(isnull(@olden),"n/a",if(entitle>@olden,"yes","no")) as higher,
  @olden:=entitle 
from table order by year;

Please note that assigning variables in a SELECT statement may not work in future MySQL versions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...