mysql奇怪的多个内连接情况

时间:2011-11-12 12:17:51

标签: php mysql inner-join

我的情况:

表“_customers”

----------------------------------------------
customer_id    |    name    |    active
----------------------------------------------
     1          'customer I'       1
----------------------------------------------
     2          'customer II'      0
----------------------------------------------

表“_projects”

----------------------------------------------
project_id    |    project_name    | customer_id
----------------------------------------------
     1             'project I'           1
---------------------------------------------- 
     2             'project II'          2
----------------------------------------------

多对多表“_project_sections”

----------------------------------------------
section_id  |  project_id
----------------------------------------------
     2             1
----------------------------------------------
     3             1
----------------------------------------------
     4             1
----------------------------------------------
     1             2
----------------------------------------------

在我的案例中,我现在需要所有“活跃”的客户。它们也必须来自特定的部分。因此,作为一个例子,我希望所有活跃的客户来自“1”部分。我也想出了正确的数据,但是 - 这就是奇怪的事情 - 只有在需要ID为>的部分时才会这样做。 1.如果我试图让第1部分中的所有活跃客户,查询仍然显示所有项目与Section_id 2,3,4 ...

查询

SELECT c.customer_id, c.name, ps.section_id
FROM _customers c
  INNER JOIN _projects p
     ON p.customer_id = c.customer_id 
  INNER JOIN _project_sections ps
    ON ps.project_id = p.project_id 
  WHERE c.active = 1 AND ps.section_id = 1
GROUP BY c.name
ORDER BY LOWER(c.name)

错误的结果:

Array
(
[customer_id] => 1
[name] => customer I
[section_id] => 1
)

也许有人可以帮助我,因为我没有得到它。非常感谢你!

2 个答案:

答案 0 :(得分:2)

您不需要GROUP BY子句来编写此查询。

首先,让我们看看活跃的客户及其项目。

-- All active customers 
SELECT c.customer_id, c.name
FROM customers c
WHERE c.active = 1;

customer_id    name
--
1              customer I

-- All active customers and their projects
SELECT c.customer_id, c.name, p.project_id, p.project_name
FROM customers c
INNER JOIN projects p ON (p.customer_id = c.customer_id)
WHERE c.active = 1;

customer_id  name          project_id  project_name  
--
1            customer I    1           project I
1            customer I    2           project II

只有一个活跃的客户,他有两个项目。

现在让我们来看看第1节项目。

-- All projects from section 1
SELECT ps.project_id, ps.section_id
FROM project_sections ps
WHERE ps.section_id = 1;

project_id    section_id
--
2             1

-- All projects from section 1, including project name
SELECT ps.project_id, p.project_name, ps.section_id
FROM project_sections ps
INNER JOIN projects p ON (p.project_id = ps.project_id)
WHERE ps.section_id = 1;

project_id    project_name    section_id
--
2             project II      1

只有一个第1节项目。现在把各个部分放在一起。

-- All active customers and their projects from section 1
SELECT c.customer_id, c.name, p.project_id, p.project_name, ps.section_id
FROM customers c
INNER JOIN projects p ON (p.customer_id = c.customer_id)
INNER JOIN project_sections ps ON (p.project_id = ps.project_id)
WHERE c.active = 1 AND ps.section_id = 1;

customer_id  name          project_id    project_name    section_id
-- 
1            customer I    2             project II      1

这就是我所期待的。

稍后。 。 。

我看到你改变了数据。如果我在这里做了相同的更改,那么上面的上一个查询将不返回任何行。我想这就是你所说的你所期待的。 (而且,随着您对数据的更改,这也是我所期望的。)

答案 1 :(得分:1)

根据您的说明,查询应为:

SELECT c.customer_id, c.name, ps.section_id
FROM _customers c
  INNER JOIN _projects p
     ON p.customer_id = c.customer_id 
  INNER JOIN _project_sections ps
    ON ps.section_id = p.project_ID 
  WHERE c.active = 1 AND ps.section_id = 1
GROUP BY c.name
ORDER BY LOWER(c.name)

特定部分的约束属于WHERE子句,而不是JOIN