Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql help needed for booking system

Posted on 2013-11-15
5
Medium Priority
?
343 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 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

688 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