Toggle navigation
TechnoWikis.com
Email or Username
Password
Remember
Login
Register
|
I forgot my password
All Activity
Questions
Hot!
Pending
Ask a Question
Privacy Policy
Contact
MySQL Conditional stored procedures IF - CASE
Home
Tutorials
Databases
MySQL Conditional stored procedures IF - CASE
(adsbygoogle = window.adsbygoogle || []).push({});
+5
votes
439
views
MySQL Conditional stored procedures IF - CASE
asked
Jun 23, 2019
in
Databases
by
backtothefuture
(
552k
points)
reopened
Jun 23, 2019
|
439
views
answer
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Add answer
Cancel
1
Answer
+3
votes
Best answer
Procedures stored with conditional IF
We will create a database and a table with comments that users make, they could be articles, posts or whatever we want.
CREATE TABLE `comments` (
`idcommentary` int (11) DEFAULT NULL,
`date` date DEFAULT NULL,
`userid` int (50) DEFAULT NULL,
`comment` text,
PRIMARY KEY (`idincidence`)
) ENGINE = MyISAM DEFAULT CHARSET = latin1;
We are going to create a stored procedure that counts the amount of comments that a user has left and returns us the level in which that user is, taking as reference for the example.
Expert
if you have made more than 5000 comments
Advanced
if you have commented between 4999 and 1000 times
Moderator
if you have commented between 999 and 500 times
Editor
if you have commented between 499 and 100 times
.
We create the stored procedure and it will have two parameters, one will be input the id or user number whose variable is p_user and the other one that will be the level with the variable p_level
.
We explain the stored procedure we call it, we assume an id = 112
CALL nivelusuario (112)
Select and count the number of comments for this user and store it in the total variable, then the structure if you assign the level to the variable p_level and return the value.
<?
// We prepare a query from php
$ result = $ mysqli-> query ("CALL nivelusuario (112)");
$ row = $ result-> fetch_assoc ();
echo 'Level:'. $ row ['p_level'];
?>
Procedures stored with conditional CASE
The operation is very similar to the previous one, only the conditional is CASE and in this example we obtain the cost of sending or surcharge according to the location of a client.
We will assume an idcity = 2 and call the stored procedure
CALL costenvio (2)
<?
// We prepare a query from php
$ result = $ mysqli-> query ("CALL costenvio (2)");
$ row = $ result-> fetch_assoc ();
echo 'Surcharge for delivery'. $ row ['p_recargo'];
?>
Also within the possibilities provided by the conditional structures, we can use the if or case structures to perform several actions with a single stored procedure
We will create for example a stored procedure to manage products to which sending a parameter we can then determine what action or task we will perform.
We can see that in the product process we have a parameter called p_accion, so we assign if the action will save a new product or modify in the database
Example to insert a new product
<?
// We prepare a query from php
$ result = $ mysqli-> query ("CALL products (10, 'Mobile phone', '100.00', 'new')");
echo 'The product has been registered';
?>
Example to insert an edit or modify a product
<?
// We prepare a query from php
$ result = $ mysqli-> query ("CALL products (10, 'MXC3 mobile phone', '120.00', 'edit')");
echo 'The product has been modified';
?>
This method is very useful for grouping tasks known as CRUD ie create, read, edit and delete data and perform updates and searches all ordered in the same file, thus facilitating maintenance and the ability to scale the database if necessary. .
It also allows from the point of view of the functional analysis to have more clear the business rules of the application.
answered
Jun 23, 2019
by
stackoverflow
(
3.5m
points)
edited
Jun 23, 2019
ask related question
comment
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Related questions
+5
votes
1
answer
MYSQL stored procedures - Creation, Queries and data insertions
asked
Jun 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
265
views
+5
votes
1
answer
Warehouse procedures with parameters from PHP
asked
Jun 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
219
views
+5
votes
1
answer
How to make a conditional formula in Excel: Use IF function in Excel
asked
Sep 12, 2023
in
Office
by
backtothefuture
(
552k
points)
|
67
views
+4
votes
1
answer
Disable MySQL Installer is running in Community mode Windows 10 CMD window | ManifestUpdate
asked
Feb 18, 2021
in
Databases
by
backtothefuture
(
552k
points)
|
397
views
+3
votes
1
answer
Uninstall MySQL on Ubuntu 21.04 | Completely
asked
Nov 13, 2020
in
Databases
by
backtothefuture
(
552k
points)
|
447
views
Sponsored articles cost $40 per post. You can contact us via
Feedback
All categories
Tutorials
7.5k
Microsoft
1.9k
Android
1.4k
Security
120
Linux / Unix
543
Internet
757
Virtualization
104
Apple
611
Networks
64
Other Devices
321
Other Applications
184
Hardware
19
Development
53
Digital Marketing
47
Databases
14
Graphic Design
30
Guides
794
GraphicDesign
54
Networking
4
PlayStation
186
Gaming
55
Linux/Unix
85
Manzana
33
Otherdevices
38
Otherapps
49
Digitalmarketing
39
Safety
1
Developing
2
Help
685
Social Networks
34
Android Tutorial
549
iPhone Tutorial
267
News
17
Social
6
Phone
11
Telephone
9
Applications
167
Smartphones
3
Cell Phones
11
Applications
25
Travels
6
Photo
21
Education
18
Games
25
Internet
14
Music
8
Technique
10
Video
6
Windows
5
Apple
2
Cell Phones
3
TikTok
216
Trips
2
House
1
Operating System
5
Tips & Tricks
892
Solutions
6
Tutorials
3
FAQ
1
Applications
5
Cell Phones
2
Tutorials
4
Computers
6
Tutoriales
2
8
Technology
2
In Computers
1
In Applications
2
Tutorials Tutorials
9
Applications Applications
25
Aplicaciones Applications
4
Devices Devices
5
Tutoriales Tutorials
1
Tutorials u00a0 Tutorials
2
Applications u00a0 Applications
2
Devices u00a0 Devices
1
OS OS
1
Etc Etc
2
Most popular questions within the last 30 days
Please leave a comment about whether the solution works or not (with device model)
[X]Close
10,659
questions
10,791
answers
510
comments
3
users