简体   繁体   English

如何在不同的列中返回相同的数据?

[英]How to return same data in different column?

I have few columns as below: 我的栏目如下:

  column1 column2 column3 column4
  ID123   Apple   Red     Apple
  ID456   Apple   Blue    Apple
  ID987   Pear    Blue    Apple
  ID899   Pear    Blue    Apple

I wanted to pull rows where column2 = column4 as below: 我想拉出column2 = column4行,如下所示:

  column1 column2 column3 column4
  ID123   Apple   Red     Apple
  ID456   Apple   Blue    Apple

I tried: 我试过了:

  select column1,column2,column3,column4
  where column2=column4

But no result are returned. 但是没有结果返回。

Where is my mistake? 我的错误在哪里?

As Recommend to you if the collation is Case Sensitive then you should make the columns all the same by using either UPPER or LOWER i would also do a replace for any spaces. 根据您的建议,如果排序规则区分大小写,则应使用UPPER或LOWER来使列相同,我也将替换任何空格。

Example

 CREATE TABLE #temp1
(
    column1 NVARCHAR(255),
    column2 NVARCHAR(255),
    column3  NVARCHAR(255),
    column4 NVARCHAR(255)
)

INSERT INTO #temp1 VALUES('ID123','Apple','Red',' Apple')
INSERT INTO #temp1 VALUES('ID456','Apple','Blue','Apple')
INSERT INTO #temp1 VALUES('ID987','Pear','Blue','Apple')
INSERT INTO #temp1 VALUES('ID899','Pear','Blue','Apple')

SELECT * FROM #temp1
WHERE REPLACE(UPPER(column2), ' ', '' ) = REPLACE(UPPER(column4), ' ', '' )

drop table #temp1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL - 如何查询相同的表、相同的列、不同的数据? - SQL - How to query for same table, same column, different data? 如何在具有相同列名的两个不同表上搜索相同数据 - How to search same data on two different tables with same column names 为同一列返回具有不同特定数据的行或使用 POSTGRESQL 返回 0 行 - Return rows with different specific data for the same column or return 0 rows using POSTGRESQL 如何有效地从同一个表中的不同列和行返回数据? - How to efficiently return data from different columns and rows in same table? 当一列有不同的数据时如何返回 1 行而不是 3 行 - How to return 1 row instead of 3 when one column has different data 如何在SQL中选择相同的列数据作为不同的行 - How to select same column data as different rows in SQL BigQuery:如何使用应用于同一列的三个不同过滤器获取数据? - BigQuery: How to get data with three different filters applied to same column? 如何通过引用同一张表的不同列值获取数据 - How to get data by using different column values refrencing same table 如何在PIVOT中使用相同的标题将列的数据拆分为不同的内容? - How to split a column's data into different with same Header in PIVOT? 如何使用不同的条件从同一列中获取数据? - How to fetch data from same column using different criteria?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM