简体   繁体   English

通过mysql将两个查询合并在一起

[英]merging two queries together via mysql

I want to merge two queries together, but I do not know how! 我想把两个查询合并在一起,但我不知道怎么做!

First query: 第一个查询:

SELECT tablesite.name,
           tablesite.family,
           job_list.job_name,
           tablesite.phone_number
           FROM  tablesite 
           INNER JOIN relation
           on tablesite.id_user=relation.user_id
           INNER JOIN job_list
           on relation.job_id=job_list.job_id 

I have jobs in this query. 我在这个查询中有工作。

第一张桌子




Second query: 第二个查询:

SELECT tablesite.name,
       tablesite.family,
       tablesite.phone_number,
       COUNT(action.service_provider_id)
       FROM tablesite
       LEFT JOIN action
       ON tablesite.id_user=action.service_provider_id
       AND action.vote !='' AND action.customer_comment =''
       GROUP BY name, family, phone_number

Result of this table is this: 该表的结果如下:

i have COUNT(action.service_provider_id) when COUNT(action.service_provider_id) in second query 我在第二个查询中有COUNT(action.service_provider_id)时有COUNT(action.service_provider_id)

第二张桌的结果

after merging these two tables, the result have to like this: 合并这两个表后,结果必须是这样的:

i made this with paint :p :D 我用油漆做了这个:p:D

决赛桌

used tables 用过的表

tablesite: tablesite:

tablesite

action: 行动:

行动

relation: 关系:

关系

job_list: job_list:

在此输入图像描述

sasha tried this: sasha试过这个: 在此输入图像描述

The results of both queries can be combined like this: 两个查询的结果可以这样组合:

CREATE TEMPORARY TABLE t1 (KEY(name,family,phone_number)) SELECT tablesite.name,
               tablesite.family,
               job_list.job_name,
               tablesite.phone_number
               FROM  tablesite 
               INNER JOIN relation
               on tablesite.id_user=relation.user_id
               INNER JOIN job_list
               on relation.job_id=job_list.job_id;    
SELECT ts.name,
           ts.family,
           ts.phone_number,t1.job_name,
           COUNT(action.service_provider_id)
           FROM tablesite ts JOIN t1 USING(name,family,phone_number)
           LEFT JOIN action
           ON ts.id_user=action.service_provider_id
           AND action.vote !='' AND action.customer_comment =''
           GROUP BY name, family, job_name, phone_number;

It is possible to avoid the use of the temporary table using a sub-query, but it would run into issues if the result set is large due to the lack of join key in the sub-query result. 可以避免使用子查询来使用临时表,但如果由于子查询结果中缺少连接键而导致结果集很大,则会遇到问题。

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

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