Solved

sql help needed for booking system

Posted on 2013-11-15
5
333 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 500 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 Comment

by:dorarishome
ID: 39652901
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now