0

I have these tables in my SQL:

___BookingsDatas

|------------|-----------------|------------|
| BOD_Id     | BOD_BookingId   | BOD_Rate   |
|------------|-----------------|------------|
| 1          | 8               | 19.00      |
| 2          | 9               | 29.00      |
|------------|-----------------|------------|

___Bookings

|------------|-----------------|------------|
| BOO_Id     | BOO_CustomerId  | BOO_RoomId |
|------------|-----------------|------------|
| 8          | 98              | 33         |
| 9          | 99              | 34         |
|------------|-----------------|------------|

___Customers

|------------|-----------------|------------|
| CUS_Id     | CUS_FirstName   | CUS_Name   |
|------------|-----------------|------------|
| 98         | Eric            | Smith      |
| 99         | David           | Black      |
|------------|-----------------|------------|

I want to link these table, so I have this query:

SELECT * FROM ___BookingsDatas INNER JOIN ___Bookings ON ___BookingsDatas.BOD_Id=___Bookings.BOO_Id;

It's working actually.

The problem is on the second table I have some information I need to link with the third one.

For example, I need to have the following results:

|--------|---------------|----------|--------|----------------|------------|--------|---------------|---------|
| BOD_Id | BOD_BookingId | BOD_Rate | BOO_Id | BOO_CustomerId | BOO_RoomId | CUS_Id | CUS_FirstName | CUS_Name|
|--------|---------------|----------|--------|----------------|------------|--------|---------------|---------|
| 1      | 8             | 19.00    | 8      | 98             | 33         | 98     | Eric          | Smith   |
| 2      | 9             | 29.00    | 9      | 99             | 34         | 99     | David         | Black   |
|--------|---------------|----------|--------|----------------|------------|--------|---------------|---------|

So, how can I use results from the first mysql query in another where statement in this case ?

Thanks.

3
  • 3
    so? you don't know how to add another one inner join to existing one? Commented Jan 5, 2017 at 18:14
  • Google "correlated sub-queries". Commented Jan 5, 2017 at 18:15
  • you might see stackoverflow.com/a/10195633/2026740 Commented Jan 5, 2017 at 20:34

1 Answer 1

1

Maybe I am not understanding the question, but could you just join to the third table?

SELECT * FROM BookingsDatas A 
JOIN Bookings B ON 
   A.BOD_Id=B.BOO_Id
JOIN Customers C ON
   B.BOO_CustomerID = C.CUS_Id
;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.