简体   繁体   中英

Comparing two mysql db tables/arrays in php and updating based on timestamp

Horrible title I know, I couldn't find a better way to articulate my problem.

I searched through SO without success, but if the answer's already out there, feel free to link it and I will read it thoroughly.


I'm building a book ordering system. People will order books throughout the month, and one large order arrives at the end of every month.

Orders get recorded into an Order table with the following fields:

order_id, book_id, quantity, language, person_ordering, timestamp, month, year

When orders arrive, they are inputted into an Received table with the following fields:

book_id, quantity, language

Now suppose one person orders (2) copies of book 1. And another person orders (3). And another (5). For a grand total of (10). Then the order arrives and it only has 7 copies.

I'm trying to write a script/function that will find out:

  • Which persons will receive their copies of the book (it's first come first serve, so the people that ordered it first will have their order fulfilled first.
  • If a person can only have their order partially fulfilled, to update the Order table (or possible a new Pending Orders table is needed?) to reflect that they have X amount still waiting to be fulfilled. Then the following month, their orders would be fulfilled first, again, based on date ordered.

I thought about pulling the information from the Orders table based on time-stamp of when the order was made and sorting through it, then pulling the information out of the Received table and somehow comparing the two arrays and updating a third array? Or perhaps there's a simpler solution that I'm missing.

If more information is needed, I will gladly provide. I've been pulling my hair out over this problem for 2 days straight. Any help would be appreciated.

Thanks!

You can try with adding an extra field in Received table. when the order is coming you can calculate the number of copies that we can provide at that time.

book_id, quantity,qty_available, language

 1. B1    2   2  EN
 2. B1    3   3  EN
 3. B1    5   2  EN

Assuming that the Received table is for current stock, and that orders are only shipped when the entire order can be shipped, here's a suggestion for what you can do:

  • Add a field to Order for whether the Order has been fulfilled.
  • Select from Order who have ordered the book, ordered by date. You can limit by quantity of Received.
  • Start fulfilling the Orders and marking them as such until quantity from Received is reached.
  • If what is left of Received quantity is not enough to fulfill an Order, leave it and leave the remaining quantity in Received for next time.

Hope it makes sense.

To me, it sounds like you could do away with your timestamp, month, and year variables to be replace with a single datetime stamp.
Also, add a column in your Received table for the datetime stamp of collection.
Finally, make another table to keep track of fulfilled orders.

  1. Do a select * on the month needed ordered by the datetime stamp descending for your "orders" data.
  2. In a separate query get your "received" data for programmatic comparison using the correct month.
  3. Put the "received" quantity of each "received" book_id into separate variables (probably a key/value array).
  4. For each "received" book_id, compare and compute the "ordered" quantity needed from each order in a sub-loop, updating the "received" quantity value. At the end of each iteration of your sub-loop, if your "ordered" quantity does not equal 0, then you need to add another entry for the next month with the remaining quantity, also enter all fulfilled orders into your new table.

* You **will not** want to modify any existing records, as you will almost certainly need this untouched in the future.

Clear as mud? :P

* Database tip - Every entry of every record should **always** have a datetime stamp and when possible, a "who did it".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM