简体   繁体   English

从两列中选择值并将它们串联到另一张表的一列中

[英]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.

相关问题 通过从两列中选择值来插入同一表中的一列 - insert by selecting values from two columns into a column from the same table 从同一表的两个不同列中选择DISTINCT值 - SELECTING DISTINCT values from two different COLUMNS of the same table 从两个表中选择多个列,其中一个表的一列具有多个where条件,并将它们按两列分组并按顺序排序 - Selecting multiple columns from two tables in which one column of a table has multiple where conditions and group them by two columns and order by one Mysql需要从table1中获取一列两次,然后通过table2的两个不同列对它们进行排序 - Mysql need to get one column two times from table1 and sort them by two different columns of table2 从一个具有不同值的表中多次选择一列 - Selecting one column multiple times from a single table with different values SQL:从db表中的一列中选择不同的值 - SQL: selecting different values from one column in db's table MySQL从表的两个不同列中选择两个DISTINCT值 - mysql selecting two DISTINCT value from two different columns from a table 如果第一列中存在第二列值,则从两列中选择值的Spark Sql查询 - spark Sql query for selecting values from two columns if second column value is present in first column 从两列中选择特定值 - Selecting Specific Values From Two Columns Sql将值从一列移动到两列不同的列 - Sql moving values from one column to two different columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM