选择并计算行数

时间:2016-06-12 17:36:29

标签: mysql sql

我有2个表,我需要在一个查询中选择和计算行,也许有人可以帮助我,我的查询是:

SELECT
    c.id, c.first_name, c.last_name, c.speciality, c.level, c.email, c.skype, c.city, 
    s.status_type, c.status_id, c.linkedin, c.link_cv, c.interview_res, c.createdAt,
    c.updatedAt, c.recruiter_id, u.first_name AS fname, u.last_name AS lname
FROM
    Candidates c
    JOIN Users u 
        ON c.recruiter_id = u.id
    JOIN Statuses s
        ON s.id = c.status_id
WHERE
    c.deleted = false

我需要计算响应我的选择并计算它们的行数。

示例输出(我想要的):

count | fname  | lname  | ..... |
---------------------------------
3     | Ihor   | Shmidt | ...   |
3     | Andre  | Agassi | ....  |
3     | Roger  | Federer| ..... |

即。我希望我选择的字段和计数

3 个答案:

答案 0 :(得分:1)

在查询的“from”部分之前,请添加count(*)。它将为您提供行数。

Select ......, count (*) from Candidates c 
JOIN Users u ON c.recruiter_id = u.id 
JOIN Statuses s on s.id = c.status_id
WHERE c.deleted = false;

答案 1 :(得分:0)

COUNT(*)作为计数或COUNT(c.id)作为计数

SELECT COUNT(*) as count,c.id, c.first_name, c.last_name, c.speciality, c.level, c.email,
    c.skype, c.city, s.status_type, c.status_id, c.linkedin, c.link_cv, c.interview_res,
    c.createdAt, c.updatedAt, c.recruiter_id, u.first_name AS fname, u.last_name AS lname 
  FROM Candidates c 
  JOIN Users u
    ON c.recruiter_id = u.id
  JOIN Statuses s
    ON s.id = c.status_id
  WHERE c.deleted = false;

答案 2 :(得分:0)

SELECT c.id, ( SELECT COUNT(*) FROM Candidates c 
JOIN Users u ON c.recruiter_id = u.id 
JOIN Statuses s ON s.id = c.status_id 
WHERE c.deleted = false ) AS count  FROM Candidates 
c JOIN Users u ON c.recruiter_id = u.id 
JOIN Statuses s ON s.id = c.status_id 
WHERE c.deleted = false ;