SQL语句在where语句错误中使用子查询

时间:2018-03-20 20:06:07

标签: mysql sql mysql-workbench

我正在尝试更新位于美国'的所有客户的电子邮件。通过引用Country表但我一直收到错误:错误代码:1054。未知列' country' in' IN / ALL / ANY子查询'

Update Customer
Set email = concat(substr(first_name,1,1), last_name,'@sakilacustomer.com.us')
where country in(Select country From customer
join address using(address_id)
join city using(city_id)
join country using(country_id)
Group by country
Having country = 'United States');

还试过......

Update Customer
Set email = concat(substr(first_name,1,1), last_name, '@sakilacustomer.com.us')
where country in(Select country From country where country = 'United States');

我正在提供可用作参考的EER图的图像

EER Diagram

3 个答案:

答案 0 :(得分:1)

这看起来很复杂。怎么样?

Update Customer c join
       address a
       using (address_id) join
       city ci
       using (city_id) join
       country co
       using (country_id)
    set c.email = concat(left(c.first_name, 1), clast_name, '@sakilacustomer.com.us')
where co.country = 'United States';

不知何故,我认为您的查询问题是customer没有country,因此错误是country in而不是子查询内部。但错误信息本身相当令人困惑。

答案 1 :(得分:0)

尝试这样的事情:

 Update a
 set a.email = concat(substr(first_name,1,1), last_name,'@sakilacustomer.com.us')
 from customer a join 
(select * from  customer a
join address b on a.address_id=b.address_id
join city c on a.city_id=c.city_id
join country d on a.country_id=d.country_id 
where country='united states'
 ) b 
 on a.customer_id=b.customer_id

答案 2 :(得分:0)

update Customer
set email = concat(substr(first_name,1,1), last_name, '@sakilacustomer.com.us')
where customer_id in (
    select customer_id
    from customer join address using(address_id)
         join city using(city_id)
         join country using(country_id)
    where country = 'United States'
);