I had to create many routines (stored procedures) for every query, how can I do the following procedures into one single procedure, same way I need to put around eight procedures like that, any idea could be helpful, thanks in advance.
Procedure 1
INSERT INTO public_holidays (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT cl.user_id, des.department_id , us.designation_id, cl.date,cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN holidays AS hol ON hol.date = cl.date
INNER JOIN users AS us ON cl.user_id = us.id
INNER JOIN designations AS des ON des.id = us.designation_id
WHERE date(cl.created_at) = cur_dat
AND TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out) = 28800;
Procedure 2
INSERT INTO public_holidays_nine (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT clo.user_id, design.department_id , uses.designation_id, clo.date,clo.clock_in, clo.clock_out
FROM clock clo
INNER JOIN holidays AS holl ON holl.date = clo.date
INNER JOIN users AS uses ON clo.user_id = uses.id
INNER JOIN designations AS design ON design.id = uses.designation_id
WHERE date(clo.created_at) = cur_dat
AND TIMESTAMPDIFF(second,clo.clock_in, clo.clock_out) = 32400;
Try this
CREATE PROCEDURE `sp_test`(IN _date datetime)
BEGIN
#Routine body goes here...
INSERT INTO public_holidays (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT cl.user_id, des.department_id , us.designation_id, cl.date,cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN holidays AS hol ON hol.date = cl.date
INNER JOIN users AS us ON cl.user_id = us.id
INNER JOIN designations AS des ON des.id = us.designation_id
WHERE date(cl.created_at) = cur_dat
AND TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out) = 28800;
INSERT INTO public_holidays_nine (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT clo.user_id, design.department_id , uses.designation_id, clo.date,clo.clock_in, clo.clock_out
FROM clock clo
INNER JOIN holidays AS holl ON holl.date = clo.date
INNER JOIN users AS uses ON clo.user_id = uses.id
INNER JOIN designations AS design ON design.id = uses.designation_id
WHERE date(clo.created_at) = cur_dat
AND TIMESTAMPDIFF(second,clo.clock_in, clo.clock_out) = 32400;
END
You can Call this Procedure as
CALL sp_test(param1)
Related
I have a database with table Plane, the table Flight, which is basically a Flight that a Plane did, then for each flight I have solved M:N table which just holds foreign keys for Flight and Destination, 2 entries in table Destination, to know the difference between Departure and Arrival, table Destination has a connection to table Type( Departure and Arrival, which is set in another table, of 2 rows, "Zacetna" = Departure, "Koncna" = Arrival ). Table Destination also has a connection to table Date which contains Datetime.
I want to Subtract Arrival and Departure time for each Flight, then Sum the times if Plane has multiple Flights.
I have tried to just Subtract them all and then sub but that didn't work, then I have tried to Group By, but with no luck.
In the attached picture.
Plane called "Falcon FX 3000" has 2 flights, idFlight 5 and 6. To get the expected result I would have to do
( Destination_6_Date - Destination_5_Date) + (Destination_4_Date - Destination_7_Date)
but none of my solutions worked
SELECT d.Ime, letD.Destinacija_idDestinacija, l.idLet,tip.Tip, t.Termin
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000';
https://i.imgur.com/5r2G9yI.png
https://i.imgur.com/3CIfbUN.png
Sorry for the ERDiagram for being in the different language
Letalo = Plane
Let = Flight
Let_Has_Destinacija = Solved M:N, holding Flight ID and Destination ID
Destinacija = Destination
Termin = Date
Tip_Destinacija = Type
EDIT
This is the successful GROUP BY query that returns valid calculations, now I just need to sum them. I am guessing this will be done with subquery, but I don't really understand that, as I am new to MySQL
SELECT d.Ime as Name, letD.Destinacija_idDestinacija as Destination, l.idLet as idFlight,tip.Tip as Type, CAST(t.Termin as DATE), timediff(Max(t.Termin), min(t.Termin))
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000'
GROUP BY l.idLet;
https://i.imgur.com/hUXZDnQ.png
EDIT 2
By using temp tables, I managed to SUM the flights times, but I have to use 2 queries for this, which is not what I want.
DROP TABLE IF EXISTS tempTime;
CREATE TEMPORARY TABLE tempTime (
SELECT timediff(Max(t.Termin), min(t.Termin)) as FlightTime
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000'
GROUP BY l.idLet);
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(FlightTime))) as FlownTime FROM tempTime;
DROP TABLE IF EXISTS tempTime;
https://i.imgur.com/HlPtgHr.png
ACCEPTED ANSWER
SELECT
`Name`,
`Destination`,
`Type`,
`Date`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`Time_diff`))) as `Tot_Diff`
FROM (
SELECT
d.`Ime` as `Name`,
letD.`Destinacija_idDestinacija` as `Destination`,
l.`idLet` as idFlight,tip.`Tip` as `Type`,
CAST(t.`Termin` as DATE) as `Date`,
timediff(Max(t.`Termin`), min(t.`Termin`)) as `Time_diff`
FROM Letalo p
INNER JOIN `Let` l
ON l.`Letalo_idLetalo` = p.`idLetalo`
INNER JOIN `Let_Has_Destinacija` letD
on letD.`Let_idLet` = l.`idLet`
INNER JOIN `Destinacija` d
on d.`idDestinacija` = letD.`Destinacija_idDestinacija`
INNER JOIN `Termin` t
on t.`idTermin` = d.`Termin_idTermin`
INNER JOIN `Tip_Destinacije` tip
on tip.`idTip_Destinacije` = `Tip_Destinacije_idTip_Destinacije`
WHERE p.Naziv = 'Falcon FX 3000'
GROUP BY l.idLet
) as `main`
I believe this should produce the result you describe. Basically, it takes the query you have, uses it as a sub-query
then sums the time differences.
SELECT
`Name`,
`Destination`,
`Type`,
`Date`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`Time_diff`))) as `Tot_Diff`
FROM (
SELECT
d.`Ime` as `Name`,
letD.`Destinacija_idDestinacija` as `Destination`,
l.`idLet` as idFlight,tip.`Tip` as `Type`,
CAST(t.`Termin` as DATE) as `Date`,
timediff(Max(t.`Termin`), min(t.`Termin`)) as `Time_diff`
FROM Letalo p
INNER JOIN `Let` l
ON l.`Letalo_idLetalo` = p.`idLetalo`
INNER JOIN `Let_Has_Destinacija` letD
on letD.`Let_idLet` = l.`idLet`
INNER JOIN `Destinacija` d
on d.`idDestinacija` = letD.`Destinacija_idDestinacija`
INNER JOIN `Termin` t
on t.`idTermin` = d.`Termin_idTermin`
INNER JOIN `Tip_Destinacije` tip
on tip.`idTip_Destinacije` = `Tip_Destinacije_idTip_Destinacije`
WHERE p.Naziv = 'Falcon FX 3000'
GROUP BY l.idLet
) as `main`
GROUP BY `Name`,`Destination`,`Type`,`Date`;
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;
I hope someone can help. I have created this stored procedure to pull some infromation from different tables and when i tun it I get many duplicates, I was wondering if there was a way of sorting it?
use ICA
if exists(
select *
from INFORMATION_SCHEMA.ROUTINES
where SPECIFIC_SCHEMA = N'ICASchema'
and SPECIFIC_NAME = N'candidateOffer'
)
drop procedure ICASchema.candidateOffer
go
create procedure ICASchema.candidateOffer #candidateID INT
as
begin
select c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
--ORDER BY co.CourseName
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints
end
go
Thanks
Graham
Following example is for creating a simple Proc_GetAllUsers stored procedure. This will solve your duplication issue.
DROP PROCEDURE IF EXISTS Proc_GetAllUsers
GO
CREATE PROCEDURE Proc_GetAllUsers( )
BEGIN
-- your sql queries
select * FROM users;
END
Go
try this:
select * from (select distinct c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints) tablealias order by CourseName
This was the working solution.
[code]SELECT c.CandidateID, c.CandidateName, ICASchema.tblOffer.OfferTypePoints, ICASchema.tblCourse.CourseName
FROM ICASchema.tblCandidate AS c INNER JOIN
ICASchema.tblCandidateOffer ON c.CandidateID = ICASchema.tblCandidateOffer.CandidateID INNER JOIN
ICASchema.tblOffer ON ICASchema.tblCandidateOffer.CandidateOfferID = ICASchema.tblOffer.OfferID INNER JOIN
ICASchema.tblCourse ON ICASchema.tblOffer.OfferID = ICASchema.tblCourse.OfferTypeID
WHERE (c.CandidateID = '3')
[/code]
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