我有一个我想要运行的查询,但是对于列表中的多个id。以下是示例代码:
`SELECT avg(t1.Percentage)
FROM
(
SELECT
checkins.id_user as userID,
count(checkins.id_check_in) as Aantal,
(SELECT COUNT(events.id_event)
FROM
events
WHERE events.id_event_group = 146
AND `start` > '2014-01-01 00:00:00') as AantalEvents,
count(checkins.id_check_in) / (SELECT COUNT(events.id_event)
FROM
events
WHERE events.id_event_group = 146
AND `start` > '2014-01-01 00:00:00') as Percentage
FROM
checkins
LEFT JOIN events ON
events.id_event = checkins.id_event
WHERE checkins.id_user IN(
SELECT checkins.id_user as userID
FROM checkins
WHERE id_event = **26276**) # < This is the ID that I want to change through a list)
AND events.id_event_group = 146
GROUP BY checkins.id_user)t1 ;`
我根据我使用不同查询定义的列表(id列表)突出显示了我想要更改的ID。这甚至可以用SQL吗?我正在使用DBeaver作为工具。
答案 0 :(得分:1)
SQL的工作原理是&#39; sets&#39;。这意味着当您在select
语句中执行函数时,它将在该语句返回的所有行上完成。
这意味着如果您只是通过in
语句或派生表来指定要过滤查询的值列表,则将返回该列表中每一行的相应值。 / p>
如果您有id_event
值的静态列表,则可以执行以下操作:
where id_event in(1,2,3,4,etc)
如果他们在一个表中,您想要应用一组过滤器来获取事件列表,那么您只需在查询中添加inner join
:
select Columns
from OtherTables
inner join checkins
on <join condition 1>
and <join condition 2>
and <checkin filter 1>
and <checkin filter 2>
SQL聚合处理数据分组。类似下面的内容将聚合返回的整个数据集,因为没有应用分组:
select sum(Col) as Total
from Table
这样的事情会为每个ID
值提供一个总计:
select ID
,sum(Col) as Total
from Table
group by ID