Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql help needed for booking system

Posted on 2013-11-15
5
Medium Priority
?
344 Views
Last Modified: 2013-11-15
Hi,
I am building a web app for booking and tourist company
I have the following table
summary_pnr
ID | PNR_ID | PNR | SEATS
1   | 123       | ABC1 | 50
2   | 123       | ABC2  | 100
3   | 124       | XYZ  | 200

and another table
booking
ID | PNR_ID
1  |  123
2  | 124




What I am trying to achieve is as following:


When a booking done the app must deduct seats from first table (summary_pnr)
but you notice there is for same booking there is some times two pnr (Passenger name list)
so system will deduct from first pnr and if it reaches 0 it start deduct from second pnr


in another word


DO Booking ----> Check which package is this ----> 1 -->if it has two PNR ID ---> Deduct from first until its zero ---> Then start deducting from the other pnr , else alert no more seats


a previous questions about the issue is:
https://secure.experts-exchange.com/Database/MySQL/Q_28283840.html
https://secure.experts-exchange.com/Database/MySQL/Q_28288478.html
0
Comment
Question by:dorarishome
  • 4
5 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 2000 total points
ID: 39652105
I would make a stored procedure to loop through the rows and extract seats as needed. Below is an example. PLEASE NOTE that it does not handle the case where you reserve more seats than are available. You should probably have a transaction and rollback for that.

CREATE TABLE pnr_summary
      (`ID` int, `PNR_ID` int, `PNR` varchar(4), `SEATS` int);
      
INSERT INTO pnr_summary
      (`ID`, `PNR_ID`, `PNR`, `SEATS`)
VALUES
      (1, 123, 'ABC1', 50),
      (2, 123, 'ABC2', 100),
      (3, 124, 'XYZ', 200);


CREATE PROCEDURE `reserve` (_pnr int, _seats int)
READS SQL DATA
BEGIN

  DECLARE no_more_rows BOOLEAN;
  DECLARE row_id int default 0; 
  DECLARE row_pnr varchar(4) default '';
  DECLARE row_seats int default 0;

  DECLARE pnr_cur CURSOR FOR
    SELECT
        ID, PNR, SEATS
    FROM pnr_summary
    WHERE pnr_id = _pnr;

  DECLARE CONTINUE HANDLER FOR NOT FOUND
    SET no_more_rows = TRUE;

  OPEN pnr_cur;

  the_loop: LOOP

    FETCH  pnr_cur
    INTO   row_id, row_pnr, row_seats;

    IF no_more_rows THEN
        CLOSE pnr_cur;
        LEAVE the_loop;
    END IF;

    IF _seats < row_seats THEN
      UPDATE pnr_summary SET seats = seats - _seats
      WHERE ID = row_id;
      SELECT row_pnr, _seats;
      CLOSE pnr_cur;
      LEAVE the_loop;
    END IF;

    IF _seats >= row_seats THEN
      UPDATE pnr_summary SET seats = 0
      WHERE ID = row_id;
      SELECT row_pnr, row_seats;
      SET _seats = _seats - row_seats;
    END IF;
   
  END LOOP the_loop;

END//

Open in new window



http://sqlfiddle.com/#!2/3925d/3
0
 

Author Comment

by:dorarishome
ID: 39652461
That's really a great answer and help,
I really need as well an alert where no more seats or seats less than booking
Also I really need a rollback function to return seats after they expire
I appreciate your great help.
Thanks you
0
 

Author Comment

by:dorarishome
ID: 39652635
Would you also please guide me how to save this procedure into mysql database using navicatmysql or cpanel sql
Thank you
0
 

Author Closing Comment

by:dorarishome
ID: 39652902
Thank you very much
please help in the next question
http://www.experts-exchange.com/Database/MySQL/Q_28295857.html
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question