Solved

sql help needed for booking system

Posted on 2013-11-15
5
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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