两张桌子的NON Equijoin

时间:2013-07-19 04:50:20

标签: sql oracle

我有2个表我需要加入这2个表但是我没有两个表中的公共列.table结构如下

CREATE TABLE customer(
        cust_id NUMBER
                                                 CONSTRAINT cus_id_pk PRIMARY KEY,
        cust_name VARCHAR2(20) NOT NULL,
        address VARCHAR2(40),
        contact_no NUMBER NOT NULL,
        email_id VARCHAR2(30));

CREATE TABLE supplier(
        supp_id NUMBER
                                                CONSTRAINT supp_id_pk PRIMARY KEY,
        supp_name VARCHAR2(20) NOT NULL,
        address VARCHAR2(40),
        contact_no NUMBER NOT NULL,
        email_id VARCHAR2(30));

问题: 创建一个SQL查询,列出所有以contact_no为开头且以9结尾的客户和供应商。

2 个答案:

答案 0 :(得分:2)

正常UNION应该这样做;

SELECT 'customer' type, cust_id id, cust_name name, address, contact_no, email_id
FROM customer WHERE contact_no LIKE '9%9'
 UNION
SELECT 'supplier' type, supp_id id, supp_name name, address, contact_no, email_id
FROM supplier WHERE contact_no LIKE '9%9'

编辑:如果你真的想要JOIN标准较弱,你可以很容易地做到这一点;

SELECT c.cust_id, c.cust_name, c.address cust_addr,
   c.contact_no cust_contact_no, c.email_id cust_email_id,
   s.supp_id, s.supp_name, s.address supp_addr,
   s.contact_no supp_contact_no, s.email_id supp_email_id
FROM customer c
JOIN supplier s
  ON c.contact_no LIKE '9%9'
 AND s.contact_no LIKE '9%9';

An SQLfiddle showing both

答案 1 :(得分:-1)

您可以这样做: -

    (SELECT * 
    FROM customer 
    WHERE contact_no LIKE '9%9')

    UNION ALL

   (SELECT * 
    FROM supplier 
    WHERE contact_no LIKE '9%9')