0

What I am trying to achieve is that In my appointment system, I want to limit people from making more than 2 appointments without completing the already booked ones.

For that I have two tables 1: customer_bookings 2: appointments

The "customer_bookings" table has three columns appointmentid, customerid & status The appointments table has id, status, bookingstart, bookingend

In the first table we have appointmentid which is id in appointments table.

In order to achieve what I want, I figured out to run the following query to fetch the latest 5 appointments

SELECT * FROM `customer_bookings` WHERE `customerId`= $idd->id AND `status`= 1 ORDER BY ID DESC LIMIT 5

Now running another query to search for the appointmentid from the first query result and checking whether the bookingstart is greater than NOW()

SELECT * FROM `appointments` WHERE id = ?? bookingStart > NOW();

Here, how do I pass the appointmentid value from the first query and search for all the 5 results or how can I build a query to display results which has

appointmentid | customerid | bookingstart | status so that I can easily check whether bookingstart is greater than now() for the customerid

I am quite new to sql and I am not able to figure out how to do it. Any help is greatly appreciated.

There are a few other questions that answers how to join two queries however I have where clause in my queries because I am passing the customerid from php to run the query to display results of just that particular customer id. I am not able to use JOIN and UNION with multiple WHERE conditions.

  • If you need only Select results, then I would recommend using Views. https://stackoverflow.com/questions/13656864/sql-create-view-from-multiple-tables – msz Jan 19 '23 at 13:50
  • I'm can't be sure if is your case, but take a look at slq JOIN to use the table relationship of your db https://www.w3schools.com/sql/sql_join.asp – Sim1-81 Jan 19 '23 at 13:51
  • Yes, thank you. Solved it using JOIN – Rohith vsn Jan 19 '23 at 19:00

0 Answers0