简体   繁体   English

如何将多个 SQL 选择合二为一

[英]How to combine multiple SQL select into one

So I have multiple tables that have a similar format to below:所以我有多个表格与以下格式类似:

user
----
user_id
first_name
last_name
group_id

group
-----
group_id
group_name

meeting
-------
meeting_id
group_id
user_id
meeting_date
completed
cancelled

I want to do a select to get all the meetings a user has completed by year, to far I have been doing 2 separate queries get all the users then loop through and get all their meetings on an annual basis:我想做一个选择来获取用户每年完成的所有会议,到目前为止我一直在做 2 个单独的查询来获取所有用户然后循环并每年获取他们的所有会议:

select 
     u.id, u.first_name, u.last_name, g.name as groupname, g.id as group_id, 
from 
     users u, group g left join group g on u.group_id=g.group_id
order 
     by g.name asc, u.first_name asc

then a simple loop and foreach user_id I do the following:然后一个简单的循环和 foreach user_id 我执行以下操作:

select yearname, SUM(total_meetings) as total_meetings
        from  ( 
         select 
                 YEAR(meeting_date) as yearname, count(*) as total_meetings
         from
                 meetings
         where 
             user_id='value_from_foreach_loop'
             completed='1' and 
             YEAR(meeting_date) > 2016 
         group by 
             YEAR(time_of_event)
       ) t
group by 
     yearname   

I end up with data I can combine into a CSV ie:我最终得到了可以组合成 CSV 的数据,即:

u.id, u.first_name, u.last_name, group_name, group_id, yearname,  total_meetings
================================================================================================
1,    bob,          Roberts,     sales,      2,        2017,      248
1,    bob,          Roberts,     sales,      2,        2018,      988
1,    bob,          Roberts,     sales,      2,        2019,      848
2,    bill,         Henry,       sales,      2,        2017,      123
2,    bill,         Henry,       sales,      2,        2018,      1
2,    bill,         Henry,       sales,      2,        2019,      23

What I really need to do is combine these into one query, I need to remove the code that is executing the query and run it as straight SQL.我真正需要做的是将这些组合成一个查询,我需要删除执行查询的代码并将其作为直接 SQL 运行。 I am stuck trying to determine how the sis possible.我被困在试图确定姐姐是如何可能的。

I am sorry if this is really obvious I haven't actually touched SQL in many years and I just inherited a project and I am not sure where I start on this.如果这真的很明显,我很抱歉,我已经很多年没有真正接触过 SQL,我只是继承了一个项目,我不确定我从哪里开始。

Thanks in advance.提前致谢。

Something like this:像这样的东西:

select 
    u.id, u.first_name, u.last_name, g.name as groupname, g.id as group_id, YEAR(m.meeting_date), COUNT(*) as total_meetings
from 
    users u
    inner join `group` g on u.group_id = g.group_id
    inner join meetings m on u.id = m.user_id
where
    m.completed = 1 and 
    m.meeting_date > '2016-01-01'
group by
    u.id, u.first_name, u.last_name, g.name, g.id, YEAR(m.meeting_date)

You don't need your double grouping counting/summing of years, and anything that you select and then run in a loop where you're just plugging an id into a where clause is a candidate for doing in the DB with a join您不需要双重分组计数/年总和,并且您选择的任何内容然后在循环中运行,您只需将 id 插入到 where 子句中,就可以使用连接在数据库中执行操作

Implicit joins, as @CaiusJard pointed out, have been deprecated for roughly the last 30 years.正如@CaiusJard 指出的那样,隐式连接在大约过去 30 年中已被弃用。 Implicit joins were formulated as a comma separated list of tables/views, and the join conditions went into the WHERE condition.隐式连接被表述为逗号分隔的表/视图列表,连接条件进入 WHERE 条件。

We nowadays use explicit joins throughout.我们现在在整个过程中都使用显式连接。 Even cross joins should explicitly be expressed with the CROSS JOIN keywords.甚至交叉联接也应该用CROSS JOIN关键字明确表达。 And don't use "group" as an object name if you can avoid it.如果可以避免,请不要使用"group"作为对象名称。 You would run into a syntax error almost in any database, as it is one of the most important keywords in SQL.您几乎在任何数据库中都会遇到语法错误,因为它是 SQL 中最重要的关键字之一。 I put it into double quotes in my example.我在我的例子中把它放在双引号中。

Having said that - all you have to do is to join , then group in one SELECT :话虽如此 - 您所要做的就是加入,然后在一个SELECT分组:

SELECT
  u.id
, u.first_name
, u.last_name
, g.name AS groupname
, g.id AS group_id
, YEAR(m.meeting_date) AS yearname
, COUNT(*) AS total_meetings
FROM
  users u
JOIN meetings m ON u.id=m.user_id AND m.completed=1
LEFT JOIN "group" g ON u.group_id = g.group_id
GROUP BY
  u.id
, u.first_name
, u.last_name
, g.name 
, g.id 
, YEAR(m.meeting_date) 
ORDER BY
  g.name ASC
, u.first_name ASC 
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM