0

我有 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 开​​头和结尾的客户和供应商。

4

2 回答 2

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';

一个同时显示.

于 2013-07-19T04:54:54.423 回答
-1

你可以这样做: -

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

    UNION ALL

   (SELECT * 
    FROM supplier 
    WHERE contact_no LIKE '9%9')
于 2013-07-19T04:56:18.800 回答