第四章 8-在update中使用子查询using subqueries in updates--Mosh my SQL

1,不知道客户的id,只知道姓名,比如我知道客户的名字是myworks,我要更改他的所有发票内容如下

update invoices
set
payment_total=invoice_total*0.5,
payment_date=due_date
where client_id=
    (select client_id
    from clients
    where name='Myworks')

多选+tab可以缩进

1 row(s) affected, 1 warning(s): 1265 Data truncated for column 'payment_total' at row 2 Rows matched: 1  Changed: 1  Warnings: 1

2,更改在CA和NY的顾客发票信息

update invoices
set
payment_total=invoice_total*0.5,
payment_date=due_date
where client_id=
    (select client_id
    from clients
    where state in ('CA','NY'))

此处出错Error Code: 1242. Subquery returns more than 1 row

因为选中子查询发现有多行

因此需要改为

update invoices
set
payment_total=invoice_total*0.5,
payment_date=due_date
where client_id in
    (select client_id
    from clients
    where state in ('CA','NY'))

3,假设没有支付日期的,我们要按照设定换掉

update invoices
set
payment_total=invoice_total*0.5,
payment_date=due_date
where payment_date is null

4,练习:

store数据库中,orders中超过3000积分的顾客,如果下过订单,就更新comments为gold customers

答案:

update orders
set comments='gold customer'
where customer_id in   #第二步 更改orders中的评论内容,范围在第一步的顾客ID内

(select customer_id
from customers
where points>3000)   #第一步 选中积分大于3000的顾客id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值