Solved

sql help needed for booking system

Posted on 2013-11-15
5
339 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 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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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