[英]Selecting values from two columns and concatenating them into a column on a different table
I'm a bit new to SQL and trying to do what my description above says. 我对SQL有点陌生,并尝试按照上面的描述进行操作。 This below is my query:
这是我的查询:
SELECT
CONCAT (first_name, last_name) INTO full_name_variable
FROM
table_1
WHERE
phone_number = new.number;
It doesn't seem to work. 它似乎不起作用。 However, when I use this query using
AS
it works. 但是,当我使用
AS
使用此查询时,它可以工作。 Like this: 像这样:
SELECT
CONCAT (first_name, last_name) AS full_name_variable
FROM
table_1
WHERE
phone_number = new.number;
I'd really appreciate the help 我非常感谢您的帮助
Use explicitly join
instead 改用显式
join
SELECT CONCAT (first_name, last_name) INTO full_name_variable
FROM table_1 t1
INNER JOIN tabke_2 t1
ON t1.phone_number = t2.number;
Or you may use INSRET INTO
statement 或者您可以使用
INSRET INTO
语句
INSERT INTO full_name_variable (full_name)
SELECT CONCAT (first_name, last_name)
FROM table_1 t1
INNER JOIN tabke_2 t1
ON t1.phone_number = t2.number;
You need to learn the difference between a variable and a column . 您需要了解变量和列之间的区别。
INTO
loads the value into a variable, that is available in the programming block you are using. INTO
将值加载到变量中,该变量在您使用的编程块中可用。 It is the same a :=
. 是相同的
:=
。 So your first version is the same as: 因此,您的第一个版本与:
SELECT full_name_variable := CONCAT(first_name, last_name)
FROM table_1
WHERE phone_number = new.number;
AS
(which is actually optional), names a column in a result set. AS
(实际上是可选的),在结果集中命名一列。
Your code appears to be in a trigger where one does not normally return a result set. 您的代码似乎是在触发器中,通常不会返回结果集。 My guess is that you want
INTO
or :=
. 我的猜测是您想要
INTO
或:=
。 I prefer :=
because I find it more intuitive for setting variables. 我更喜欢
:=
因为我发现它对设置变量更直观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.