2

我是 SQL 开发的新手。所以我想从SO那里得到一些帮助。

我有三个表:studentstudent_addressesstudent_phones

他们的架构大致如下:

student
-------
student_id (Primary Key)
student_name
father_name
mother_name

student_addresses
-----------------
student_address_id (Primary Key)
student_id (Foreign Key)
address
zip_code

student_phones
--------------
student_phone_id (Primary Key)
student_id (Foreign Key)
phone_type
phone_number

student_addressesstudent_phones都是has_many 关系。因此,我想从student中为特定的student_id选择所有字段,但仅从 student_addresses 和student_phones中为该student_id选择匹配的计数(总数) 。我怎么得到那个?

我试过这个查询,但它返回一个错误:

SELECT students.student_id,student_name,father_name,mother_name,
       COUNT(student_addresses.student_id) AS total_addresses,    
       COUNT(student_phones.student_id) AS total_phones
 FROM students,student_phones,student_addresses
 WHERE students.student_id = student_phones.student_id AND
       students.student_id = student_addresses.student_id AND
       students.student_id = 7;

PS:目前我在 PostgreSQL 上使用它。但是,我也想在 MySQL 上使用它。那么这是否意味着我需要有两个不同的查询?AFAIK,为此,只需一个查询就可以在两者上工作(因为 MySQL 和 PostgreSQL 都遵循相同的 SQL 实现,就这个查询要求而言)。

我想知道,如果我不使用 GROUP BY 就可以做到这一点。因为,假设学生表有更多字段,比如 12,那么我将不得不将所有字段名称都放入 SELECT 和 GROUP BY(AFAIK),这似乎有点不雅。

4

3 回答 3

1

这应该适用于 MySQL 和 PostgreSQL:

SELECT s.student_id,
       max(s.student_name) student_name,
       max(s.father_name) father_name,
       max(s.mother_name) mother_name,
       COUNT(distinct a.student_address_id) total_addresses,    
       COUNT(distinct p.student_phone_id) total_phones
FROM students s
LEFT JOIN student_phones p ON s.student_id = p.student_id
LEFT JOIN student_addresses a ON s.student_id = a.student_id
WHERE s.student_id = 7
GROUP BY s.student_id
于 2013-07-23T09:40:24.080 回答
1

只需添加GROUP BY

SELECT students.student_id,student_name,father_name,mother_name,
       COUNT(student_addresses.student_id) AS total_addresses,    
       COUNT(student_phones.student_id) AS total_phones
 FROM students,student_phones,student_addresses
 WHERE students.student_id = student_phones.student_id AND
       students.student_id = student_addresses.student_id AND
       students.student_id = 7
 GROUP BY students.student_id,student_name,father_name,mother_name;

但是,如果发生 id 为 7 的学生没有地址或没有电话号码,它将不返回任何结果。即使在这种情况下也要返回某些内容,请尝试使用LEFT JOINs:

SELECT students.student_id,student_name,father_name,mother_name,
       COUNT(student_addresses.student_id) AS total_addresses,    
       COUNT(student_phones.student_id) AS total_phones
 FROM students
 LEFT JOIN student_phones ON students.student_id = student_phones.student_id
 LEFT JOIN student_addresses ON students.student_id = student_addresses.student_id
 WHERE students.student_id = 7
 GROUP BY students.student_id,student_name,father_name,mother_name;
于 2013-07-23T09:22:53.753 回答
1

你忘了包括GROUP BY

SELECT students.student_id,student_name,father_name,mother_name,
           COUNT(student_addresses.student_id) AS total_addresses,    
           COUNT(student_phones.student_id) AS total_phones
     FROM students,student_phones,student_addresses
     WHERE students.student_id = student_phones.student_id AND
           students.student_id = student_addresses.student_id AND
           students.student_id = 7
    GROUP BY BY students.student_id,student_name,father_name,mother_name;
于 2013-07-23T09:23:04.050 回答