sql - BigQuery:使用子查询和内部联接的计数更新行
问题描述
我有以下两个表:
Table 1: Students
name:string,
age:integer,
class:integer,
number_of_allowed_trips:integer
Table 2: Trips
allowed_age:integer,
trip_name:string,
class:integer
我正在尝试根据某些条件为每个学生分配允许的旅行次数。我的查询是:
UPDATE
`mydataset.students` AS s
SET
s.number_of_allowed_trips=(
SELECT
COUNT(*)
FROM
`mydataset.trips` AS t
WHERE
t.r.class= 9)
FROM
`mydataset.trips` AS t
WHERE
r.name = 'Jack'
AND r.class = 9
AND w.class = r.class
但是这个查询不起作用。我在做什么假?我怎样才能让这个代码工作?
解决方案
UPDATE
`mydataset.students` AS s
SET
s.number_of_allowed_trips= (
SELECT
COUNT(*)
FROM
`mydataset.trips` AS t
WHERE
t.class = s.class
AND s.age= 9 )
WHERE
s.id=s.id
这很好用!
推荐阅读
- c++ - C++中无与伦比的组的位置是什么?
- python - 如何解决错误:h5py 构建轮子失败
- sql - 从 2 个表中,获取每个表唯一的行
- amazon-s3 - 现有 S3 存储桶的 Terraform 数据源在计划阶段尝试返回 NoSuchWebsiteConfiguration 的 GetBucketWebsite 请求失败
- sql - 将 2 列,同一个表链接到另一个表
- entity-framework-core - EF Core 自引用多对多双方
- python - 如何在 Python 中为多个变量快速添加 1?
- elasticsearch - 将日志 prestashop 发送到 kibana
- .net-core - 我可以做些什么来加快我使用 NBomber 的负载测试?(VS LT 250 RPS 轻松;NBomber 最高 25 RPS)
- python - 熊猫:将元素与另一个数据框进行比较