user7347588
user7347588

Reputation:

Use results from one sql query in another where statement

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.

Upvotes: 0

Views: 333

Answers (1)

SaggingRufus
SaggingRufus

Reputation: 1834

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
;

Upvotes: 1

Related Questions