How to store a SELECT value in a MYSQL variable?

Asked

Viewed 4,641 times

1

It’s something very simple but I can’t find the information I want, people!

I saw once how to do it but it was very shallow, but I’ll explain what I want:

I have a variable and I want it to receive the value of a certain column that will come with a select...

set @CPF = blablabla;
select * from userdocuments where Description = @CPF;
set @userID = Aqui eu quero armazenar o valor da coluna User_id que virá com o select acima;

How do I make this variable @userID receive the column value?

2 answers

2

In the :

select @userID := User_id from userdocuments where Description = @CPF;

or

select User_id into @userID from userdocuments where Description = @CPF;

1

Thus

set @userID = (select User_id from userdocuments where Description = @CPF)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.