我有以下数据表:
Name Expression
FirstName NULL
LastName NULL
FullName CONCAT(FirstName,LastName)
Gender NULL
FullNameWGender CONCAT(FullName,Gender)
如何使用SQL语句生成从表中获取的以下数据:
FirstName, LastName, CONCAT(FirstName,LastName) AS FullName, Gender, CONCAT(CONCAT(FirstName,LastName),Gender) AS FullNameWGender
我正在使用MySQL。
我不希望完整的代码,但至少是一个起点,因为我不知道如何确保一旦我到达具有CONCAT的表达式列中的数据并处理它。
与上面的例子类似,在表中它是CONCAT(FullName,...)
。但是使用我的SQL,我必须提取并处理它以显示为CONCAT(CONCAT(FirstName,LastName),....)
...
任何帮助都会很棒!
答案 0 :(得分:0)
您可以为FullNameWGender做些什么是CONCAT(FirstName,LastName,Gender)。
Select FirstName, LastName, CONCAT(FirstName,LastName) AS FullName, Gender, CONCAT(FirstName, LastName,Gender) AS FullNameWGender
CONCAT可用于两个以上的字符串。 dev.mysql.com
答案 1 :(得分:0)
好吧,我有游标形式的代码(我别无选择)
使用与上面相同的表但有更多数据:
ID NAME EXPRESSION
1 FirstName, NULL
2 LastName, NULL
3 FullName, CONCAT( FirstName, LastName)
4 Gender, NULL
5 FullNameWGender, CONCAT( FullName, Gender)
6 Income, NULL
7 Expense, NULL
8 Savings, ( Income,- Savings,)
9 FullNameWSavings, CONCAT( FullName, Savings,)
10 Month, NULL
11 FullNameWSavingsByMonth, CONCAT( FullNameWSavings, Month,)
获取更新的表格如下:
ID Name Expression
1 FirstName, NULL
2 LastName, NULL
3 FullName, CONCAT( FirstName, LastName)
4 Gender, NULL
5 FullNameWGender, CONCAT(CONCAT( FirstName, LastName) Gender)
6 Income, NULL
7 Expense, NULL
8 Savings, ( Income,- Savings,)
9 FullNameWSavings, CONCAT(CONCAT( FirstName, LastName)( Income,- Savings,))
10 Month, NULL
11 FullNameWSavingsByMonth, CONCAT(CONCAT(CONCAT( FirstName, LastName)( Income,- Savings,)) Month,)
仅供任何人参考(希望它可以提供帮助(非常感谢@Nappa The Saiyan的答案),以下是代码:
DECLARE @id_name INT
DECLARE @id_expression INT
DECLARE @expression NVARCHAR (MAX)
DECLARE @cutExpression NVARCHAR (100)
DECLARE @name NVARCHAR (100)
DECLARE @newExpression NVARCHAR (MAX)
DECLARE @temp NVARCHAR (MAX)
DECLARE @rowNumExp INT
DECLARE @rowNumName INT
DECLARE @rowNum INT
SET @rowNum = 0
SET @rowNumExp = 0
SET @rowNumName = 0
DECLARE expressionList CURSOR FOR
SELECT ID, EXPRESSION FROM table1
OPEN expressionList
FETCH NEXT FROM expressionList
INTO @id_expression, @expression
WHILE @@FETCH_STATUS = 0
BEGIN
SET @rowNumExp = @rowNumExp + 1
IF (@expression IS NOT NULL)
BEGIN
DECLARE nameList CURSOR FOR
SELET ID, NAME FROM table1
OPEN nameList
FETCH NEXT FROM nameList
INTO @id_name, @name
WHILE @@FETCH_STATUS = 0
BEGIN
IF(CHARINDEX(@name,@expression)>0)
BEGIN
SET @cutExpression = SUBSTRING(@expression,
CHARINDEX(@name,@expression),
LEN(@name))
SET @temp = (SELECT EXPRESSION FROM table1 WHERE ID = @id_name)
IF(@temp IS NOT NULL)
BEGIN
SET @newExpression = REPLACE (@expression, @name, @temp)
UPDATE table1
SET EXPRESSION = @newExpression
WHERE ID = @id_expression
SET @expression = @newExpression
CLOSE nameList
DEALLOCATE nameList
DECLARE nameList CURSOR FOR
SELECT ID, NAME FROM table1
OPEN nameList
FETCH NEXT FROM nameList
INTO @id_name, @name
END
END
SET @rowNumName = @rowNumName + 1
FETCH NEXT FROM nameList
INTO @id_name, @name
END
CLOSE nameList
DEALLOCATE nameList
END
FETCH NEXT FROM expressionList
INTO @id_expression, @expression
END
CLOSE expressionList
DEALLOCATE expressionList
限制:
无论Expression中可用的“Name”的数量是什么,它没有NULL作为自己的表达式,该东西会将所有内容格式化为上面的第二个表。