I have the following snippet
select user_id into @selected_user from users where mail = 'mymail@gmail.com';
select * from kc_network_user where user_id = 'previous_id';
which I want to modify to be parametric. I found the following resources:
- Using variables in SQL script
- How to declare a variable in MySQL?
- Declare variables - MySQL Workbench gives me an error with DECLARE
That's what I did:
select @user_mail := 'mymail@gmail.com'; #1
select @user_mail; #2
select user_id into @selected_user from user where mail = @user_mail; #3
select @selected_user;
select * from user_network where user_id = @selected_user; #4
In select #1
I tried also the following:
set @user_mail = 'mymail@gmail.com';
The select #2
returns the correct value, so the variable is initialized.
The select #3
returns the following error:
Error Code: 1267. Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='
While the select #4
runs correctly (if I set the mail parameter with the literal value).
What's the difference between those queries? How can I set a variable before doing a query?