I have two queries that I want to join
SELECT b.nickname,count(a.doctor_id)
FROM a_table a
join b_table b
on (a.doctor_id=b.id_user)
GROUP BY a.doctor_id
and
SELECT doctor_id, SUM(medicine) medicine, SUM(radiology) radiology,sum(lab) lab, sum(act) act
FROM (
SELECT MAX(doctor_id) doctor_id,
SUM(IF(pm='F', cost, NULL)) medicine,
SUM(IF(pm='R', cost, NULL)) radiology,
SUM(IF(pm='L', cost, NULL)) lab,
SUM(IF(pm='P', cost, NULL)) act
FROM c_table
GROUP BY Trans_No
) t
GROUP BY doctor_id
I already tried to join
SELECT b.nickname, count(a.doctor_id), SUM(medicine) medicine, SUM(radiology) radiology,sum(lab) lab, sum(act) act
FROM (
SELECT MAX(doctor_id) doctor_id,
SUM(IF(pm='F', cost, NULL)) medicine,
SUM(IF(pm='R', cost, NULL)) radiology,
SUM(IF(pm='L', cost, NULL)) lab,
SUM(IF(pm='P', cost, NULL)) act
FROM c_table
GROUP BY Trans_No
) t
join a_table a on (a.doctor_id=t.doctor_id)
join b_table b on (a.doctor_id=b.id_user)
GROUP BY a.doctor_id
all of these table has a large amount of data (more than 2 million data), I already set my CommandTimeout = 600 but it still has no respond
did I join it wrong or it’s just to many data? what am I supposed to do to get the result?
This is not an answer, but it is too long for a comment.
Your queries do not make sense. I think you need to look again at the problem. The first query:
is always going to return 1 for
count(a.doctor_id), except when doctor_id is NULL. This is because you are grouping on that column. Do you mean to get the count of doctors for each nickname or the count of nicknames for each doctor?The subquery in the second query also doesn’t make much sense:
Why are you grouping the trans_no? Why aren’t you using it as an index?
Perhaps you should write another question, explaining what you are trying to accomplish.