sql语句,急..
有这样两张表
[code="java"]
表A
CREATE TABLE A (
id number(10) NOT NULL,
product_id number(10) not null ,
company_id number(10) ,
name varchar2(50) not null,
keyword varchar2(50) ,--关键词
content_markup varchar2(50) ,
sort_number number(4) ,
ad_type varchar2(50) ,
is_vitual number(1) not null,
description varchar2(255) ,
biz_state varchar2(20) ,
sys_deleted number(1) default 0 not null,
sys_ts_first number(38) ,
sys_ts_last number(38) ,
PRIMARY KEY (id)
) ;
表B
CREATE TABLE B(
id number(10) not null,
keyword varchar2(50) not null ,--关键字
synonyms varchar2(100) not null--对应同义词
) ;
[/code]
要求是当输入查询条件keyword时就到表A去找这个keyword,如果查询所输入的在表A中找
不到,就到表B去匹配它的同义词(如:输入‘咖啡’但在表B的synonyms中有‘咖啡厅’、‘咖啡连锁店’)
如果匹配到了,就把匹配到的表B的synonyms字段对应的keyword给查询出来再去匹配表A中的keyword字段判断是否有这个keyword,如果有查询出来!
貌似要把最后一句 not in 改成 in
这个写存储过程吧.
直接写sql好象搞不定这么复杂的逻辑.
select * from A where keyword='33'
union
select * from A where keyword in
(
select synonyms from B where keyword='33' and 0 not in (select count(*) from A where keyword='33')
)
一个10行当存储过程就能解决问题,而且运行速度还快。
何必纠结在一个sql语句上。