Using Defined Variables in MySQL (example)

It's been a long time coming, but I've finally learnt how to make use of variables in MySQL.
In my example, I wanted to find the maximum tk, and then use that maximum tk to display tk and fullDate (for that maximum tk.) Here is how I did it:

mysql> SET @var = (SELECT max(tk) FROM dwh_capacity_efficiency.date_dimension);

mysql> SELECT tk,fullDate FROM dwh_capacity_efficiency.date_dimension where tk = @var;
+-----+---------------------+
| tk  | fullDate            |
+-----+---------------------+
| 358 | 2017-05-08 00:00:00 |
+-----+---------------------+

Image: Example of how to use a variable in MySQL


Comments