Skip to content Skip to sidebar Skip to footer

Select And Count Rows

I have 2 tables and I need to select and count rows in one query, maybe somebody can help me with that, my query is: SELECT c.id, c.first_name, c.last_name, c.speciality, c.l

Solution 1:

Before the "from" portion of the query, please add count (*). It will give you the count of the rows.

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;

Solution 2:

COUNT(*) as count or COUNT(c.id) as count

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;

Solution 3:

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 ;

Post a Comment for "Select And Count Rows"