SQL SQL在Select语句中合并两列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5247381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:37:56  来源:igfitidea点击:

SQL Combine Two Columns in Select Statement

sqlsql-server-2005

提问by atrljoe

If I have a column that is Address1 and Address2 in my database, how do I combine those columns so that I could perform operations on it only in my select statement, I will still leave them separate in the database. I would like to be able to do this

如果我的数据库中有一个列是 Address1 和 Address2,我如何组合这些列,以便我只能在我的 select 语句中对其执行操作,我仍然会将它们在数据库中分开。我希望能够做到这一点

WHERE completeaddress LIKE '%searchstring%'

Where completedaddress is the combination of Address1 and Address2. searchstring would be like the data they searched for. So if they had '123 Center St' in Address1 and 'Apt 3B' in Address2, how would I have it select it if the searchstring was 'Center St 3B' Is this possible with SQL?

其中 Completedaddress 是 Address1 和 Address2 的组合。searchstring 就像他们搜索的数据一样。因此,如果他们在 Address1 中有“123 Center St”,在 Address2 中有“Apt 3B”,那么如果搜索字符串是“Center St 3B”,我将如何选择它 SQL 这可能吗?

回答by pavanred

I think this is what you are looking for -

我想这就是你要找的——

select Address1+Address2 as CompleteAddress from YourTable
where Address1+Address2 like '%YourSearchString%'

To prevent a compound word being created when we append address1 with address2, you can use this -

为了防止在我们将 address1 附加到 address2 时创建复合词,您可以使用这个 -

select Address1 + ' ' + Address2 as CompleteAddress from YourTable 
where Address1 + ' ' + Address2 like '%YourSearchString%'

So, '123 Center St' and 'Apt 3B' will not be '123 Center StApt 3B' but will be '123 Center St Apt 3B'.

因此,“123 Center St”和“Apt 3B”不会是“123 Center StApt 3B”,而是“123 Center St Apt 3B”。

回答by anothershrubery

In MySQL you can use:

在 MySQL 中,您可以使用:

SELECT CONCAT(Address1, " ", Address2)
WHERE SOUNDEX(CONCAT(Address1, " ", Address2)) = SOUNDEX("Center St 3B")

The SOUNDEXfunction works similarly in most database systems, I can't think of the syntax for MSSQL at the minute, but it wouldn't be too far away from the above.

SOUNDEX函数在大多数数据库系统中的工作方式类似,我暂时想不出 MSSQL 的语法,但它不会离上面太远。

回答by user3003449

SELECT StaffId,(Title+''+FirstName+''+LastName) AS FullName 
FROM StaffInformation

Where do you write with in the brackets this will be appear in the one single column. Where do you want a dot into the middle of the Title and First Name write syntax below,

你在括号中写在哪里,这将出现在一个单独的列中。你想在下面的标题和名字写语法的中间加一个点,

SELECT StaffId,(Title+'.'+FirstName+''+LastName) AS FullName 
FROM StaffInformation

These syntax works with MS SQL Server 2008 R2 Express Edition.

这些语法适用于 MS SQL Server 2008 R2 Express Edition。

回答by Sachin Shanbhag

If your address1 = '123 Center St'and address2 = 'Apt 3B'then even if you combine and do a LIKE, you cannot search on searchstring as 'Center St 3B'. However, if your searchstring was 'Center St Apt', then you can do it using -

如果你address1 = '123 Center St'address2 = 'Apt 3B'那么即使你结合,做了LIKE,你不能在搜索字符串的搜索'Center St 3B'。但是,如果您的搜索字符串是'Center St Apt',那么您可以使用 -

WHERE (address1 + ' ' + address2) LIKE '%searchstring%'

回答by Shane Delmore

If you don't want to change your database schema (and I would not for this simple query) you can just combine them in the filter like this: WHERE (Address1 + Address2) LIKE '%searchstring%'

如果你不想改变你的数据库模式(我不会为这个简单的查询),你可以像这样在过滤器中组合它们: WHERE (Address1 + Address2) LIKE '%searchstring%'