她考虑这个城市查询中的条件的原因是什么?
SELECT salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;
查询的目的是寻找同城的客户和卖家。可以在表中的FK字段上写查询吗?
SELECT salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;
可能吗?是的(至少我是这么认为)。
示例数据(类似 Oracle,但忽略它)
SQL> with
2 salesman (name, city) as
3 (select 'Little', 'Berlin' from dual union all --> Scott lives here
4 select 'Foot' , 'London' from dual union all --> Mike lives here
5 select 'Big' , 'Zagreb' from dual
6 ),
7 customer (name, city) as
8 (select 'Scott' , 'Berlin' from dual union all --> Little lives here
9 select 'Tiger' , 'Bern' from dual union all
10 select 'Mike' , 'London' from dual --> Foot lives here
11 )
查询返回什么?
12 select s.name salesman,
13 c.name customer,
14 s.city they_live_in
15 from salesman s join customer c on c.city = s.city;
SALESMAN CUSTOMER THEY_LIVE_IN
---------- ---------- ---------------
Little Scott Berlin
Foot Mike London
SQL>
对我来说,看起来还不错。