Problem joining separate tables

Hello everyone,

could someone please help me with a query?

I have one large table consisting of a number of foreign key columns.

The join I’ve used is like this:

$q = “SELECT column names
FROM bigtable AS big INNER JOIN
table1 AS T1 ON T1.some_id=big.some_id INNER JOIN
table2 AS T2 ON T2.another_id=big.another_id
…WHERE category = ‘$category’”;

In the above query I’m joining all of the tables to the “bigtable”, but now I’d like to also join two other tables in the same query. These two tables only join one another, the one table has a foreign key from the “bigtable” (if this is relevant).

I tried the following but it doesn’t work too well:

$q = “SELECT column names
FROM bigtable AS big INNER JOIN
table1 AS T1 ON T1.some_id=big.some_id INNER JOIN
table2 AS T2 ON T2.another_id=big.another_id INNER JOIN
other_table1 AS OT1 INNER JOIN
other_table2 AS OT2 ON OT2.other_id = OT1.other_id
…WHERE category = ‘$category’”;

I hope this makes sense.

Thank you very much!

“the one table has a foreign key from the “bigtable” (if this is relevant).”

SELECT column names
FROM bigtable AS big INNER JOIN
table1 AS T1 ON T1.some_id=big.some_id INNER JOIN
table2 AS T2 ON T2.another_id=big.another_id INNER JOIN
other_table1 AS OT1 ON OT1.somekey = big.someFK INNER JOIN
other_table2 AS OT2 ON OT2.other_id = OT1.other_id

best i could do, given all the fake names

INNER JOIN
other_table1 AS OT1 INNER JOIN

You are missing an ON condition here. Unless you want to do a cross join?

Hi Rudy and Guido,

thank you for your assistance. I think it works but I’ve noticed a problem.

“the one table has a foreign key from the “bigtable” (if this is relevant).”

This table with the foreign key is a “lookup” table (or whatever the term is), which means that the same foreign key can appear multiple times in the table. At one point my query loops out some data but it now loops out the same data multiple times. If the foreign key appears 5 times in the lookup table, the data will be looped out 5 times.

Do you know what I can do to prevent this?

Thanks!

you could maybe not let your query loop?

it’s really really hard to figure out what you’re doing with all those fake table and column names clogging up the query

Hi Rudy,

sorry for the confusion, this is what I’m trying to do:

$q = "SELECT column names
FROM hotels AS h INNER JOIN
rooms AS r ON r.hotel_id=h.hotel_id INNER JOIN
hotels_features AS hf ON hf.hotel_id = h.hotel_id INNER JOIN
features AS f ON f.features_id = hf.features_id
...WHERE category = '$category'";

I need to loop out the rooms but because the hotel_id appears multiple times in the hotels_features table, the rooms will loop out multiple times.

Thank you.

okay, i understand the problem

but the solution is not to tinker with the query

you need to write a loop within a loop when processing the query results

perhaps ask this question in the php forum, or whatever language you’re using

Okay, thanks Rudy.

Thread moved to PHP to save people from having to jump back and forth…

Hi everyone,

it still doesn’t work, it seems that the hotel_id in the lookup table is definitely the problem. As a last resort, must I use another query?

$q = "SELECT column names
FROM hotels AS h INNER JOIN
rooms AS r ON r.hotel_id=h.hotel_id INNER JOIN
hotels_features AS hf ON hf.hotel_id = h.hotel_id INNER JOIN
features AS f ON f.features_id = hf.features_id
...WHERE category = '$category'";

    $r = @mysqli_query($dbc, $q);
    $row = mysqli_fetch_array($r, MYSQLI_ASSOC);


// echo data here


$r2 = @mysqli_query ($dbc, $q);
while ($row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {

// loop content here
}

// continuing echoing data here

Thank you!

please tell me you didn’t actually run it like that… with “column names” in the SELECT clause

Of course I don’t actually use the words “column names” in my query. I’d have the table alias.column names such as r.room_name, f.features_name etc…

I know my skills are limited by come on…:slight_smile:

okay, then :slight_smile:

my skills as a psychic are limited, so please explain what exactly does “doesn’t work” mean?

Surely you of all people would know what problem I’m talking about?

I need to loop out the rooms but because the hotel_id appears multiple times in the hotels_features table, the rooms will loop out multiple times.

not until you tell me

and FYI, i don’t do php, but they tell me it’s not all that complicated

in my application language (coldfusion), i don’t have any trouble getting loops within loops to spit out the correct results

I think the problem lies with the query, and not with PHP. The query creates a Cartesian product, hence the multiplication.

But how to avoid the multiplication?

yeah, i can see the problem now that i stop and think about the probable cardinalities – a hotel has multiple rooms, and a hotel also has multiple features

you will have to collapse one of the multiplicities in a subquery

if i knew the extent of the columns being returned (instead of “column names”) then i’d be able to recommend which of them would be easier

Hi Rudy,

I’ve changed my query and am now looping out the hotels, together with the features of every hotel. There are plenty more columns in my hotel table but to simplify things I’ve only included the hotel name here. The problem now is that the same hotel is looping out however many times the hotel_id appears in the lookup table.

$q = “SELECT h.hotel_name, f.features_name
FROM hotels AS h INNER JOIN
hotels_features AS hf ON hf.hotel_id = h.hotel_id INNER JOIN
features AS f ON f.features_id = hf.features_id”;

Thanks for your help. This is driving me nuts.

this query looks correct

yes, obviously, if a hotel has multiple features, then the hotel name will appear multiple times in the query results

like i said, i don’t do php, but looping over the results and only showing each hotel name once is trivial

Okay, thanks Rudy.

Perhaps someone else will know how to do this?
I’m using a standard while loop to loop out all of the hotels:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

}