将搜索参数显示为列

时间:2014-01-03 01:29:42

标签: sql oracle oracle11g

目前我在'Like'语句中将搜索参数显示为1个新列时遇到问题。我的SQL代码如下:

 Select A.Description from table A Where A.Description like ('%Battery%')

输出:

Description 
12V Battery
 9V Battery
 5V Battery

我想要的输出是:

Description   Description_1
12V Battery   Battery
 9V Battery   Battery
 5V Battery   Battery

不可能这样做吗?

3 个答案:

答案 0 :(得分:0)

如何将其包含在SELECT声明中?

SELECT  A.Description ,
       'Battery' AS Description_1
FROM    table A 
WHERE   A.Description LIKE '%Battery%'

答案 1 :(得分:0)

类似的东西:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name ( Description ) AS
          SELECT '9V Battery' FROM DUAL
UNION ALL SELECT '12V Battery' FROM DUAL
UNION ALL SELECT '24V Battery' FROM DUAL
UNION ALL SELECT 'Torch' FROM DUAL
UNION ALL SELECT 'Rope' FROM DUAL;

查询1

WITH search_terms AS (
            SELECT 'Battery' AS search_term FROM DUAL
  UNION ALL SELECT 'Torch' FROM DUAL
)
SELECT A.Description,
       s.search_term AS Description_1
FROM   table_name A
       INNER JOIN
       search_terms s
       ON ( A.Description LIKE '%' || search_term || '%' )

<强> Results

| DESCRIPTION | DESCRIPTION_1 |
|-------------|---------------|
|  9V Battery |       Battery |
| 12V Battery |       Battery |
| 24V Battery |       Battery |
|       Torch |         Torch |

或者,如果您只使用单个搜索字词,则可以使用bind variable替换指定的子查询:

SELECT A.Description,
       :search_term AS Description_1
FROM   table_name A
WHERE  A.Description LIKE '%' || :search_term || '%'

答案 2 :(得分:0)

这个查询怎么样:

with tab as
(select 'Battery' FILTER
   from dual)
Select A.Description, B.filter
  from table_name A, tab B 
 Where A.Description like '%'|| b.filter ||'%';

输出

| DESCRIPTION |  FILTER |
|-------------|---------|
|  9V Battery | Battery |
| 12V Battery | Battery |
| 24V Battery | Battery |

将过滤器放入查询select 'Battery' FILTER from dual