SQL将一个表连接到另一个表两次

时间:2014-04-08 07:33:24

标签: mysql sql join

我有这个用于通过连接3个表,sample_register,villages和water_cssr获取数据的sql。

SELECT
  sample_register.location,
  sample_register.description,
  villages.distance,
  case when water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as total_cs,
  case when water_cssr.bdl='Y'and water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as bdl_cs,
  case when water_cssr.bdl='N'and water_cssr.istp='Cs' then min(water_cssr.activity) end as csmin,
  case when water_cssr.bdl='N'and water_cssr.istp='Cs' then max(water_cssr.activity) end as csmax,
  case when water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as total_sr,
  case when water_cssr.bdl='Y'and water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as bdl_sr,
  case when water_cssr.bdl='N' and water_cssr.istp='Sr' then min(water_cssr.activity) end as srmin,
  case when water_cssr.bdl='N' and water_cssr.istp='Sr' then max(water_cssr.activity) end as srmax
FROM sample_register
LEFT JOIN villages on sample_register.location=villages.location
LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin
INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
GROUP BY sample_register.location, sample_register.description, sample_allocation.cs 
order by villages.dist_group, villages.location

我得到了这样的结果

Location   Type    Distance   Total_cs   Bdl_cs   Csmin   csmax   Total_sr   Bdl_sr   Srmin   srmax
A          TYPE1   5                                               1         1
B          TYPE2   10         1          1        4       12
B          TYPE2   10                                              1         1        1       8
C          TYPE3   15                                              1         1        9       14
C          TYPE3   15         1          1        15      24
D          TYPE1   10                                              1         1
E          TYPE2   10         1          1
F          TYPE1   20         1          1                       

在上面的位置B和C有两行,每行有一行在第4到第7列有值,另一行在第8到第11列有值。我希望这两行的内容在一行中,因为第一列到第三列的值对于两行都是通用的。例如,第二行和第三行应该产生一个像这样的行

B   TYPE2   10  1   1   4   12  1   1   1   8

请帮我重构SQL

2 个答案:

答案 0 :(得分:0)

这只是一个概念:

  • 查找哪些表或表连接为B和C返回2行。
  • 为该表或表连接创建查询,因此它将返回1行 对于b和c以及其他记录。
  • 然后修改您的查询以在联接中使用新查询,如果正确b 并且c不再返回2行。
祝你好运

答案 1 :(得分:0)

我做了两个子查询并像这样加入

 SELECT * FROM  (SELECT
sample_register.location,
sample_register.description,
villages.distance, villages.direction,
case when water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as total_cs,
case when water_cssr.bdl='Y'and water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as bdl_cs,
case when water_cssr.bdl='N'and water_cssr.istp='Cs' then min(water_cssr.activity) end as csmin,
case when water_cssr.bdl='N'and water_cssr.istp='Cs' then max(water_cssr.activity) end as csmax
FROM
sample_register LEFT JOIN villages on sample_register.location=villages.location LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
 WHERE mid(sample_register.usin,3,1)='N' and  sample_register.doc between '$start_date' and '$end_date' AND water_cssr.istp='Cs'  GROUP BY sample_register.location, sample_register.description, sample_allocation.cs order by villages.dist_group, villages.location) AS a left JOIN (SELECT
sample_register.location,
sample_register.description,
villages.distance, villages.direction,
case when water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as total_sr,
case when water_cssr.bdl='Y'and water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as bdl_sr,
case when water_cssr.bdl='N'and water_cssr.istp='Sr' then min(water_cssr.activity) end as srmin,
case when water_cssr.bdl='N'and water_cssr.istp='Sr' then max(water_cssr.activity) end as srmax
FROM
sample_register LEFT JOIN villages on sample_register.location=villages.location LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
 WHERE mid(sample_register.usin,3,1)='N' and  sample_register.doc between '$start_date' and '$end_date' AND water_cssr.istp='Sr'  GROUP BY sample_register.location, sample_register.description, sample_allocation.cs order by villages.dist_group, villages.location) as b on a.location=b.location and a.description=b.description"