MySQL数据库查询解决方案

时间:2015-03-05 07:04:11

标签: mysql

鉴于数据库架构如下

employee(employee_name, street, city);
works(employee_name,company_name,salary);
company(company_name,city);

现在我需要一个查询 1.找到所有住在纽约市的员工,但他们的公司不在纽约市。

3 个答案:

答案 0 :(得分:1)

使用以下查询

select e.employee_name from employee e
Join Works w on e.employee_name = w.employee_name
Join company c on w.company_name = c.company_name
Where e.city like "New York" and c.city not like "New York"
order by e.employee_name

答案 1 :(得分:0)

请尝试以下查询

select emp.employee_name  from employee emp left join works w on emp.employee_name =w.employee_name left join company c on w.company_name = c.company_name

where isnull( emp.city,'') like 'New York' and  isnull( company.city,'')!= 'New York'

答案 2 :(得分:-1)

从员工e中选择e.employee_name e.employee_name = w.employee_name加入公司c w.company_name = c.company_name其中e.city如“New York”和c.city不喜欢“New York” “按e.employee_name排序