MySQL: Using user defined variable

This entry is part 15 of 26 in the series Learn MySQL

Here we will save a value produced by an expression so that we can refer to it in a subsequent statement.

In the earlier table, we have price, artiste and album columns.

select @p := price from artist where artiste = 'New Order';
select album FROM artist where price = @p;

We first assign the price into a variable @p.  Using this variable, we can find the album with this price.

Clicking on the first line in the result set will show the @p variable



Clicking on the second line will show the resulting album selected.




Series Navigation<< MySQL: Show databases and show tables
MySQL: Select and display a variable >>

Leave a comment

Leave a Reply