2012-08-27 24 views
5

Tôi có một truy vấn mysql:Làm thế nào để tổng hợp các bản ghi của một cột với các khả năng khác nhau?

SELECT count(*) as `present_days` 
FROM tbl_intime_status 
WHERE employee_status = 'Out' and 
     present_status = 'Full Day' and 
     date LIKE '%/"+month2+"/"+year1+"' and 
     employee_id="+ EmpId+ 

Từ truy vấn này tôi nhận được không. trong số Full Day hiện tại.

Tôi có present_status= 'Half Day' & present_status = 'Full Day' trong hồ sơ cơ sở dữ liệu của mình.

Cách đếm 'Cả ngày' + 'Nửa ngày'?

Trả lời

4

Nếu bạn muốn tính riêng biệt, bạn có thể làm điều này

SELECT present_status, count(*) as `present_days` 
FROM tbl_intime_status 
WHERE employee_status = 'Out' and 
     present_status IN ('Full Day','Half Day') 
     date LIKE '%/"+month2+"/"+year1+"' and 
     employee_id="+ EmpId+ 
GROUP BY present_status 

Nếu bạn muốn tổng của cả hai làm được điều này

SELECT count(*) as `present_days` 
FROM tbl_intime_status 
WHERE employee_status = 'Out' and 
     present_status IN ('Full Day','Half Day') 
     date LIKE '%/"+month2+"/"+year1+"' and 
     employee_id="+ EmpId+ 
+0

@ hol: thanks..Its working – Harshali

1

Chỉ cần thêm nó vào mệnh đề where của bạn, hoặc với IN:

... 
WHERE present_status IN ('Full Day','Half Day') 
... 

hoặc với OR:

... 
WHERE (present_status = 'Full Day' OR present_status = 'Half Day') 
... 
1

phải là những gì này, bạn đang tìm kiếm:

SELECT count(*) as present_days 
FROM tbl_intime_status 
WHERE employee_status = 'Out' 
and present_status IN ('Full Day', 'Half Day') 
and date LIKE '%/"+month2+"/"+year1+"' 
and employee_id="+ EmpId 
3

thử, don'tforget để thoát khỏi cột date vì nó là một từ dành riêng.

SELECT present_status, count(*) as `present_days` 
FROM tbl_intime_status 
WHERE employee_status = 'Out' and 
     present_status IN ('Full Day','Half Day') 
     `date` LIKE '%/"+month2+"/"+year1+"' and 
     employee_id = " + EmpId + " 
GROUP BY present_status 
2

tôi muốn thử này để xem nếu nó hoạt động:

... 

present_status in ('Full Day', 'Half Day') 

... 

Ngữ pháp có thể được tìm thấy ở đây: SQL IN Operator.

0

này sẽ cung cấp cho bạn chia tay của tất cả các loại tội trong một hàng:

SELECT SUM(IF(present_status = 'Full Day', 1, 0)) AS full_present_days, 
     SUM(IF(present_status = 'Half Day', 1, 0)) AS half_present_days, 
     COUNT(*) AS present_days 
FROM tbl_intime_status 
WHERE employee_status = 'Out' and 
     present_status IN('Full Day', 'Half Day') and 
     date LIKE '%/"+month2+"/"+year1+"' and 
     employee_id="+ EmpId+; 
0
SELECT SUM(
    CASE present_status 
    WHEN 'Full Day' THEN 1 
    WHEN 'Half Day' THEN 0.5 
    END 
) as `present_days` 
FROM tbl_intime_status WHERE employee_status = 'Out' 

Hy vọng đây là truy vấn bạn đang tìm kiếm.

Các vấn đề liên quan