Query Based on conditional Association statements in oracle

Source: Internet
Author: User


In some businesses, you may need to connect the joined table to the master table to obtain the desired result. This is a bit like Conditional compilation. join is performed only when the criteria are met. If the criteria are not met, no join is performed (or the association condition fails). Environment: oracle, please refer to the Code: SQL code create table A1 (id number, val number); create table A2 (id number, type varchar2 (2), val number, anyvalue varchar2 (30); insert into a1 values (1, 50 ); insert into a1 values (1,500); insert into a1 values (2,100); insert into a1 values (2,150); insert into a1 values (3,200); insert into a1 values (3,250 ); insert into a1 values (4,150); insert into a1 values (4,500); insert into a2 values (1, '>', 100, '> 100 '); insert into a2 values (1, '<=', 100, '<= 100'); insert into a2 values (2, '<', 150, '<150 '); insert into a2 values (3, '<= ', 200,' <= 200 '); insert into a2 values (4,'> = ', 150, '> = 150'); select * from a1; select * from a2; select * from a1, a2 where a1.id = a2.id and (case a2.type when '> 'then (case when a1.val> a2.val then 1 else 0 end) when '> = 'then (case when a1.val> = a2.val then 1 else 0 end) when' <= 'then (case when a1.val <= a2.val then 1 else 0 end) when' <'then (case when a1.val <a2.val then 1 else 0 end) else 0 end) = 1; drop table a1 purge; drop table a2 purge; author czwlucky

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.