MySQL Stored Procedure Conditional Statement - mysql

Can anyone help me on how could I have an conditional statement on the MySQL stored procedure?
I have this as sample query,
If my stored procedure parameter isFirstTask = true then I will use LEFT JOIN else I will use INNER JOIN
SELECT *
FROM jobdetails jd
INNER JOIN taskslogs tl ON jd.jobID = tl.jobid;
The question is how could change the INNER JOIN into LEFT JOIN without repeating the whole query just to replace one word. Assuming that my query is bulk. Is it possible? Some idea please.

SELECT *
FROM jobdetails jd
LEFT JOIN taskslogs tl ON jd.jobID=tl.jobid
WHERE IF(isFirstTask = TRUE, TRUE, tl.jobid IS NOT NULL);

you can build the query as a string inside the stored procedure. While building the query, check the value of your parameter and decide which JOIN to use. Hope this helps.

Related

MySql SP is not working

I am new in mysql I am trying to get data using below SP where I used if else statements
CREATE DEFINER=`Travel_user`#`%` PROCEDURE `new_procedure`(in bus_id int, in travel_id int, in tr_date Date, in Seat_no varchar(45))
BEGIN
select #SeatNo := Fare from Travel_booking.SpecialFares where TravelId=travel_id and date=tr_date ;
IF (#SeatNo = NULL) then
select #SeatNo := sd.Fare from SeatDetails as sd
inner join tbl_ColumnDetails as c on c.ColumnId=sd.ColId
inner join tbl_RowsDetails as r on r.RowId=c.RowID
inner join BusStructure as bs on bs.StructId=r.StructureId
inner join BusDetails as bd on bd.Structure=bs.StructId
where sd.IsActive=1 and bd.BusiId=bus_id and sd.travelid=travel_id and sd.SeatNo=Seat_no;
END IF;
END
I am calling it like
call Travel_booking.new_procedure(34, 53, '2018-03-01', 'L2');
First select query is working fine and its showing proper result.
But if statement giving blank result when the result of first query is blank.
I have check both query separately like below they both working.
First query,
select Fare from Travel_booking.SpecialFares
where TravelId=53 and date='2018-03-02';
Second query,
select sd.Fare from SeatDetails as sd
inner join tbl_ColumnDetails as c on c.ColumnId=sd.ColId
inner join BusStructure as bs on bs.StructId=r.StructureId
inner join BusDetails as bd on bd.Structure=bs.StructId
where sd.IsActive=1 and bd.BusiId=34 and sd.travelid=53 and sd.SeatNo='L2';
But when I execute them in SP and passing parameters to them so I am getting blank result in if statement in same query.
Thanks to everyone for responding and helping me to find out where i am wrong.
I got my answer
Just simply declaring null variable at the start of SP like
BEGIN
select #SeatNo := NULL;
select #SeatNo := Fare from Travel_booking.SpecialFares where TravelId=travel_id and date=tr_date ;....
My Suggestions would be.
1) You have many joins please take a look if each join has any values and if these joins are correctly connected, meaning the joins are of equal value.
2) Check your parameters, In you SQL environment, check if your parameters produces results or not.
3)A tip would also be, check if your parameters is being passed correctly. Creating a output would instantly tell you, the developer of what is wrong with your code.
IF (#SeatNo is NULL) then
select #SeatNo := sd.Fare from SeatDetails as sd
inner join tbl_ColumnDetails as c on c.ColumnId=sd.ColId
inner join tbl_RowsDetails as r on r.RowId=c.RowID
inner join BusStructure as bs on bs.StructId=r.StructureId
inner join BusDetails as bd on bd.Structure=bs.StructId
where sd.IsActive=1 and bd.BusiId=bus_id and sd.travelid=travel_id and sd.SeatNo=Seat_no;
END IF;
write is instead of '=';

Query to find Number of Parameters in a Stored Procedure or Function in Sql Server?

Well, if i want to find parameter count of any stored procedure or function inside SQL SERVER, what is the correct way to do it.
Your help would be appreciated.
thanks.
Try the following query to get a list of all parameters for a stored procedure. Change the select to a COUNT(*) if you just want the number of parameters.
SELECT
p.name AS Parameter,
t.name AS [Type]
FROM sys.procedures sp
JOIN sys.parameters p
ON sp.object_id = p.object_id
JOIN sys.types t
ON p.system_type_id = t.system_type_id
WHERE sp.name = '<name>'
INFORMATION_SCHEMA.PARAMETERS should be all you need...
SELECT *
FROM INFORMATION_SCHEMA.PARAMETERS
SELECT *
FROM INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME = 'YourProcedureName'

How to use a SQL Function with INNER JOIN in MySQL?

I have a function "fnc_FindIssueId" which accepts an object id and return its assigned issue Id.
When I call the function using pure select statements, it works fine:
select fnc_FindIssueId(150083); // returns 1 as issueId for objectId of 150083
select fnc_FindIssueId(150072); // returns 2 as issueId for objectId of 150072
But when I use it within an Inner Join, it goes into a never-ending loop:
select so.id, si.id
from smart_objects as so
LEFT OUTER join smart_issues as si
on si.id = fnc_FindIssueId(so.id)
where so.id in (150083, 150072);
What's the reason and how to resolve it?
It does not perform never-ending loop.
The reason for that is because the server performs FULL TABLE SCAN which is very slow. This condition si.id = fnc_FindIssueId(so.id) doesn't use an index even if you have define one on si.id and so.id.
The best ways you can do are:
to alter the table smart_objects
another column for the assigned issue Id
define an index on the new column
The workaround was to create a new view with ObjectId and IssueId columns then calling that function from within that view! but it has become very slow now.
CREATE ALGORITHM=UNDEFINED DEFINER=`mysql`#`%` SQL SECURITY DEFINER VIEW `vw_smart_objectissue` AS select `so`.`id` AS `objectid`,`fnc_FindIssueId`(`so`.`id`) AS `issueid` from `smart_objects` `so` order by `so`.`id`$$

how to write sql query without alias

Is it possible to write this sql query without alias? I am using a PHP script that doesn't covers alias so I have problem with that.
If this is possible please provide me with some help
This is the code:
SELECT
time1.Time, time2.Time, time1.Signal, v.name, v.lastname, k.vehicle, time1.Reg
FROM
data time1
INNER JOIN data time2
ON time1.id != time2.id
AND time1.serial= time2.serial
INNER JOIN drivers v
ON time1.FK_ID_driver=v.ID_driver
INNER JOIN vehicles k
ON time1.Reg=k.Reg
WHERE
TIMEDIFF(time2.Time, time1.Time) BETWEEN '00:15:00' AND '00:30:00';
You can't easily achieve what you want, since you are joining the same table twice, and SQL needs an alias to disambiguate them.
You could, however, create a view for table data, and use the view instead of the table name in one of the data joins.
Example:
select data.time,
vData.time,
data.Signal,
drivers.name,
drivers.lastname,
vehicles.vehicle,
data.Reg
from data
inner join vData on data.id != vData.id and data.serial = vData.serial
inner join drivers on data.FK_ID_driver = drivers.ID_driver
inner join vehicles on data.Reg = vehicles.Reg
where TIMEDIFF(vData.time, data.time) between '00:15:00' and '00:30:00';

MYSQL get other table data in a join

I am currently running this SQL
SELECT jm_recipe.name, jm_recipe.slug
FROM jm_recipe
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
This returns the desired results except that I also need to return the name of the category that the recipe I am looking for is in, to do this I tried to add the field in to my SELECT statement and also add the table into the FROM clause,
SELECT jm_recipe.name, jm_recipe.slug, jm_category_name
FROM jm_recipe, jm_category
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
However this just returns no results, what am i doing wrong?
You need to join both tables:
SELECT jm_recipe.name, jm_recipe.slug, jm.category_name
FROM jm_recipe
INNER JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
INNER JOIN jm_category ON jm_recipe.recipe_id = jm_category.recipe_id
WHERE jm_category_recipe.category_id = $cat
I've changed the joins to inner joins as well. You might want to make them both LEFT joins if you have NULLs and want them in the result.
Also, you're vulnerable to SQL Injection by simply copying over $cat.
Here's some PHP specific info for you (I'm assuming you're using PHP.)