SQL Query获取数据

时间:2013-07-23 09:20:58

标签: mysql sql database postgresql

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

我有三张桌子:学生 student_addresses student_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_addresses student_phones 都是has_many关系。所以我想选择学生中的所有字段来获取特定的 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)上,这看起来有点不雅。

3 个答案:

答案 0 :(得分: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 JOIN s:

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;

答案 1 :(得分: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;

答案 2 :(得分: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