Skip to content
Advertisement

how can i fetch the name from id by using this query?

there are 2 table tbl_hk and tbl_hk_work:

  1. tbl_hk:hk_id(pk), hk_name

  2. tbl_hk_work:hk_work_id, hk_id(fk), hk_work_date, hk_work_1bed, hk_work_2bed, hk_work_time

and I have written a query to fetch sum of hk_work_1bed, hk_work_2bed from hk_id from two dates inputted by the user and I also want to fetch hk_name with the same query but its showing error the only help I need is everything is perfect I just want hk_name to be fetched with the SUM.

QUERY:

SELECT SUM(hk_work_1bed), SUM(hk_work_2bed), 
  SUM(hk_work_time_decimal) 
FROM tbl_hk_work 
WHERE (hk_work_date BETWEEN '2019-10-1' AND '2019-10-1') 
  AND hk_id=1;

Advertisement

Answer

all you need is to join your tbl_hk table and group by hk_name

select t2.hk_name, sum(hk_work_1bed), sum(hk_work_2bed), sum(hk_work_time_decimal) 
from tbl_hk_work t1
inner join tb_hk t2 on t2.hk_id = t1.hk_id
where hk_work_date between '2019-10-1' and '2019-10-1' and t2.hk_id=1
group by t2.hk_name;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement