简体   繁体   English

MySQL查询有两个连接

[英]MySQL query with two joins

I have query I need to perform in MySQL on three linked tables. 我有查询我需要在三个链表上执行MySQL。 I can do it the lazy way with nested queries but I cannot work out how to do it with a single query. 我可以用嵌套查询做懒惰的方式,但我无法弄清楚如何使用单个查询来完成它。

Tables are: 表是:

Area:-
 : id     (int)
 : name   (string)

Consultant:-
 :id      (int)
 :active  (1/0)

ConsArea:-
 : areaID        (int)
 : consultantID  (int)

I need to loop through all the areas (with $area variable) so that I list ALL areas and against each area to indicate the number of 'active' consultants ... so all areas must be listed with a value next to it (which can be zero if there are no active consultants associated) 我需要遍历所有区域(使用$area变量),以便列出所有区域和每个区域以指示“活动”顾问的数量...因此所有区域必须列在其旁边的值(其中如果没有相关的活跃顾问,则可以为零)

The first part of the query (irrespective of consultant active or not) I can do with: 查询的第一部分(无论顾问是否活跃)我可以做:

      SELECT areas.name AS aname, COUNT(consAreas.areaID) AS cct 
        FROM areas LEFT OUTER JOIN consAreas 
          ON consAreas.areaID = areas.id 
       WHERE areas.areaID = $area 
    GROUP BY areas.id 
    ORDER BY areas.name

.. but when I want to include the condition of the consultant being active I cannot work out the correct join. ..但是当我想要包括顾问活跃的条件时,我无法确定正确的连接。 It only lists the areas with > 0 active consultants whereas I need all areas. 它只列出了> 0活跃顾问的区域,而我需要所有区域。

      SELECT areas.name AS aname, COUNT(consAreas.area) AS cct 
        FROM areas LEFT OUTER JOIN consAreas 
          ON consAreas.area = areas.id 
        **JOIN consultants ON consultants.id = consAreas.cons**
       WHERE areas.areaID = $area 
         **AND consultants.active = 1**
    GROUP BY areas.id 
    ORDER BY areas.name

Anyone help? 有人帮吗?

This is because of a behaviour of mysql. 这是因为mysql的行为。 An inner join following a left join makes that left join an inner join. 左连接后的内连接使左连接成为内连接。

SELECT areas.name AS aname, COUNT(consultants.id) AS cct 
FROM areas
    LEFT JOIN consAreas ON consAreas.area = areas.id 
    LEFT JOIN consultants ON consultants.id = consAreas.cons AND consultants.active = 1
WHERE 
    areas.areaID = $area 
GROUP BY areas.id 
ORDER BY areas.name

Here you can see I'm only using left joins, and more importantly filtering the consultants.active status from the left join ON clause directly. 在这里你可以看到我只使用左连接,更重要的是直接从左边连接ON子句过滤consultants.active状态。

What you want here is a LEFT JOIN (aka left outer join). 你想要的是LEFT JOIN (又名左外连接)。

A JOIN (really an inner join) selects a row resulting from the JOIN only if it has corresponding rows in both tables. JOIN (实际上是一个内连接)只有在两个表中都有相应的行时才会选择JOIN产生的行。 A left join will select rows if only the left table has a matching row, regardless of whether the right one does. 如果左表只有匹配的行,则左连接将选择行,无论右表是否匹配。

So in your join to the consultants table: 所以在你加入顾问表时:

SELECT areas.name AS aname, COUNT(consAreas.area) AS cct 
FROM areas LEFT OUTER JOIN consAreas 
ON consAreas.area = areas.id 
LEFT JOIN consultants ON consultants.id = consAreas.cons

You need to do a LEFT JOIN and correct the GROUP BY . 您需要执行LEFT JOIN并更正GROUP BY Try this: 尝试这个:

SELECT areas.name AS aname, COUNT(consultants.active) AS cct 
    FROM areas 
    LEFT JOIN consAreas 
      ON consAreas.area = areas.id 
    LEFT JOIN consultants 
      ON consultants.id = consAreas.cons
   WHERE areas.areaID = $area 
     AND consultants.active = 1
GROUP BY areas.name
ORDER BY areas.name

This will return a table with the name of area and the number of active consultants 这将返回一个表格,其中包含区域名称和活跃顾问的数量

Try this: 尝试这个:

SELECT areas.name AS aname, COUNT(consultants.id) AS cct 
    FROM areas LEFT OUTER JOIN consAreas 
      ON consAreas.areaID = areas.id 
    LEFT OUTER JOIN consultants ON consAreas.consultantID=consultants.id AND consultants.active = 1
   WHERE areas.areaID = $area 
     GROUP BY areas.id 
ORDER BY areas.name

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

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