I have two tables: calls and employees. In the calls tables I have a field enter_emp_id and another follow_emp_id. They hold the values for which employee entered the call and which employee is assigned to the call. My second table employees has employee_id and employee fields. I am able to write a query to show results with an employee name for who entered the call, but not who it is assigned to. I do have other values in the query, but I need to get the 2nd employee name to show in the results. Here is what I am using so far:
SELECT
`calls`.`call_id`,
`calls`.`enter_date`,
`call_state`.`call_state`,
`customers`.`customer_name`,
`calls`.`comments`,
`employees`.`employee`,
`call_reasons`.`call_reason`
FROM
`customers`
INNER JOIN `calls` ON (`customers`.`customer_id` = `calls`.`customer_id`)
INNER JOIN `employees` ON (`employees`.`employee_id` = `calls`.`enter_emp_id`)
INNER JOIN `call_state` ON (`call_state`.`call_state_id` = `calls`.`call_state_id`)
INNER JOIN `call_reasons` ON (`call_reasons`.`call_reason_id` = `calls`.`call_reason_id`)
WHERE
`calls`.`call_id` = $call_id;
You need to join to the employee table TWICE... once for each employee id association, and use the ALIAS of the respective to get the values intended. And aliasing the tables and removal of unnecessary tick marks not required. Only needed for possible reserved word conflicts.
SELECT
c.call_id,
c.enter_date,
cs.call_state,
cust.customer_name,
c.comments,
entered.employee,
assigned.employee as AssignedEmployee,
cr.call_reason
FROM
calls c
INNER JOIN customers cust
ON c.customer_id = cust.customer_id
INNER JOIN employees entered
ON c.enter_emp_id = entered.employee_id
INNER JOIN employees assigned
ON c.enter_emp_id = assigned.employee_id
INNER JOIN call_state cs
ON c.call_state_id = cs.call_state_id
INNER JOIN call_reasons cr
ON c.call_reason_id = cr.call_reason_id
WHERE
c.call_id = $call_id;
I hope i understand you problem. You can use AS to change or shorten table name for sql usage
SELECT
`calls`.`call_id`,
`calls`.`enter_date`,
`call_state`.`call_state`,
`customers`.`customer_name`,
`calls`.`comments`,
`employees`.`employee`,
`assigned`.`employee` as assigned_employee,
`call_reasons`.`call_reason`
FROM
`customers`
INNER JOIN `calls` ON (`customers`.`customer_id` = `calls`.`customer_id`)
INNER JOIN `employees` ON (`employees`.`employee_id` = `calls`.`enter_emp_id`)
INNER JOIN `employees` AS `assigned` ON (`assigned`.`employee_id` = `calls`.`follow_emp_id`)
INNER JOIN `call_state` ON (`call_state`.`call_state_id` = `calls`.`call_state_id`)
INNER JOIN `call_reasons` ON (`call_reasons`.`call_reason_id` = `calls`.`call_reason_id`)
WHERE
`calls`.`call_id` = $call_id;
Thanks the following worked:
SELECT
c.call_id,
c.enter_date,
cs.call_state,
cust.customer_name,
c.comments,
entered.employee,
entered.employee,
cr.call_reason
FROM
calls c
INNER JOIN customers cust
ON c.customer_id = cust.customer_id
INNER JOIN employees entered
ON c.enter_emp_id = entered.employee_id
INNER JOIN employees assigned
ON c.enter_emp_id = assigned.employee_id
INNER JOIN call_state cs
ON c.call_state_id = cs.call_state_id
INNER JOIN call_reasons cr
ON c.call_reason_id = cr.call_reason_id
WHERE
c.call_id = $call_id;
Related
I'm trying to run a query but my outcome is not what i need.
So the problem is:
A user can be diretor and if this is the case he can see all activities from his department, and he can be user of another department too, in this case not director but only user.
I have 8 departments each with one director, so the following query should give me the activities of the department and the activities of this particular user in other department:
SELECT t1.idAtividade,
t1.idProfessor,
t2.Escola,
t1.Atividade,
t1.Periodo,
t1.Mes,
t1.haveClasses,
t1.DataPrevista,
t1.Destinatarios,
t1.Orcamento,
t1.PdfAtividade,
t1.Avaliacao,
t1.PdfAvaliacao,
t1.idProfessor,
p.Nome,
g.Grupo,
d.Departamento,
p2.Projeto,
t1.idProjeto
FROM atividades AS t1
INNER JOIN professores p on t1.idProfessor = p.idProfessor
INNER JOIN atividadesgrupos ag on t1.idAtividade = ag.idAtividade
INNER JOIN grupos g on ag.idGrupo = g.idGrupo
INNER JOIN departamentosatividades da on t1.idAtividade = da.idAtividade
INNER JOIN departamentos d on da.idDepartamento = d.idDepartamento
INNER JOIN escolas AS t2 ON (t2.idEscola = t1.idEscola)
INNER JOIN anosescolares AS ae ON (t1.idAnoEscolar = ae.idAnoEscolar)
INNER JOIN projetos p2 on t1.idProjeto = p2.idProjeto
WHERE ae.Estado = 1 AND (da.idDepartamento = :id_dpt and ag.idGrupo = :idGrupo)
ORDER BY (t1.idProfessor = :idProfessor) DESC, t1.idProfessor;");
This query is not working because the department have 22 activities but this user (idProfessor) has 5 in this department and 14 more in another department (defined by idGrupo)
I think i will need a subquery right?
I have four tables by the name candidate, program_type, program_of_interest and desired_intake. The table candidate has a many to one relationship with the rest of the three tables.
The candidate table looks like this:
The program_type table:
The program_of_interest table:
The desired_intake table:
I am trying to replace the program_type,program_of_interest,desired_intake id's in the candidate table with the corresponding values in their respective tables.
I am able to select the values as needed with help from this thread SQL Replace multiple variables from another table in query result. Here is my solution to select:
SELECT
c.id,
p.value as 'Program type',
p1.value as 'Program of interest',
d.value as 'Desired intake'
FROM candidate c
JOIN program_type p on p.id = c.program_type
JOIN program_of_interest p1 on p1.id = c.program_of_interest
JOIN desired_intake d on d.id = c.desired_intake
My question is how do I replace the ids in the candidate table with their respective values?
You could use an update basend on join as
Update candidate c
INNER JOIN program_type p on p.id = c.program_type
INNER JOIN program_of_interest p1 on p1.id = c.program_of_interest
INNER JOIN desired_intake d on d.id = c.desired_intake
set c.program_type = p.value,
c.program_of_interest = p1.value,
c.desired_intake = d.value
but seems strange you want update eg: program_type with the value (c.program_type = p.value)
and you are using program_type for join ( p.id = c.program_type)
I have searched for days on how to get around this error while trying to update a field from a multiple join table, with a minimum date from the same mutiple join tableset.
This is my Update statement:
update vtiger_projectmilestone
Inner Join vtiger_projectmilestonecf ON vtiger_projectmilestone.projectmilestoneid = vtiger_projectmilestonecf.projectmilestoneid
Inner Join vtiger_crmentity ON vtiger_projectmilestone.projectmilestoneid = vtcrmm.crmid
inner join vtiger_project on vtiger_project.projectid = vtiger_projectmilestone.projectid
Inner Join vtiger_crmentity vtcrmp ON vtcrmp.crmid = vtiger_project.projectid
set vtiger_projectmilestone.projectmilestonedate =
(select min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
)
where vtiger_projectmilestone.projectid =
(select vtiger_project.projectid from vtiger_project
INNER JOIN vtiger_crmentity vtcrmp ON vtiger_project.projectid = vtcrmp.crmid
where vtcrmp.deleted = 0 order by vtiger_project.projectid desc limit 1)
and vtcrmp.deleted = 0
and vtcrmm.deleted = 0
and (vtiger_projectmilestone.projectmilestonedate is null or vtiger_projectmilestonecf.cf_763 is null) ;
This is a real life update query, not just a simple one table relationship.
I got round it by creating a temp table, inserting the value, updating the destination table and dropping the temp table.
I would really like to get this right, because it will come up more often.
All assistance is appreciated.
Cheers
Bernard Bailey
As stated in this answer you can't use the target update table in a subquery, as you can see in your query
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join
--using target update table in query
vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
However i think that a work around is using the data from the table that you're updating, so instead of using joins, you could write some where conditions for example:
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_crmentity AS vtcrmm ON tvpt.projecttasknumber = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
--using the projectmilestoneid in a where clause
AND tvpt.projecttasknumber=vtiger_projectmilestone.projectmilestoneid
The caveat could be that probably you will get some performance issues, also, as I don't know the full schema, I can't tell if using other tables in the subquery instead of vtiger_projectmilestone will give you the right result
I am trying to run this query bug it keeps failing; can't use table in the from clause.
update student s1 set tot_cred = (select total_cred from student s inner join taken t on s.id=t.id inner join transfer_course tc on (t.transfer_course_id, t.college_id) = (tc.transfer_course_id, tc.college_id));
Any pointers appreciated!
Thanks
Try this instead:
I don't think the 'on' keyword allows multiple columns to be compared.
If it does it is news to me
update student s1
set tot_cred = (select total_cred
from student s
inner join taken t
on s.id=t.id
inner join transfer_course tc
on t.transfer_course_id = tc.transfer_course_id
and t.college_id = tc.college_id)
update student s1
set s1.tot_cred =
(select total_cred
from student s
inner join taken t
on s.id=t.id
inner join transfer_course tc
on (t.transfer_course_id = tc.transfer_course_id)
AND (t.college_id = tc.college_id)
);
I have a problem in a query.
i need to make a page where user can see all reservations he made with movie name, cinema name, seats code that he reserved
i reached this level
SELECT member.member_username, show_datetime, movie_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
WHERE `reservation`.`member_id`=1
i need to make a connection and get also the cinema_name from cinema table and seats_code from seat table and theater name
in fact, i need a query to give me all almost all data in my whole database
here is the schema for the DB
http://imageshack.us/photo/my-images/824/58054944.jpg/
JOIN these two tables too:
SELECT
m.member_username,
sh.show_datetime,
v.movie_name,
c.cinema_name,
t.theater_name
FROM member AS m
INNER JOIN reservation AS r ON r.member_id = m.member_id
INNEr JOIN show AS sh ON sh.show_id = r.show_id
INNER JOIN movie AS v ON v.movie_id = s.movie_id
INNER JOIN theater AS t ON t.theater_id = sh.theater_id
INNER JOIN cinema AS c ON c.theater_id = sh.theater_id
WHERE r.member_id = 1
Keep joining your tables
SELECT member.member_username, show_datetime, movie_name, c.cinema_name, t.theater_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
JOIN `theater` ON `show`.`theater_id` = `theater`.`theater_id`
JOIN `cinema` ON `theater`.`cinema_id` = `cinema`.`cinema_id`
JOIN `seat` ON `show`.`theater_id` = `seat`.`theater_id`
WHERE `reservation`.`member_id`=1