Jump to content

Joining tables


jeff5656

Recommended Posts

 

I hope this is not confusing. 

Lets say I want to join 2 tables.

 

When I put it through a while loop, how do I write the code so that all id's in table2 are on the SAME line for each referring id in table 1, before dropping to the next line and going to the next record in table 1?

 

I THINK that actually makes sense if you read it carefully :-)

Link to comment
https://forums.phpfreaks.com/topic/220252-joining-tables/
Share on other sites

Ok here's kind of the code I would want

$query = "SELECT * FROM items INNER JOIN mainrecords ON items.record_id = mainrecords.id_incr AND items.user_id = '$userid' "; 

$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
  echo echo  items.item1 from all the records in the items table that have record_id equal to mainrecords.id_incr;
"<br>"
} end while

These are the tables:

mainrecord:

id_incr | name

1        | johnson

2          | smith

etc.

 

 

items:

record_id  | item

1              | flashliht

1              | radio

2              | phone

etc.

 

so when I do the while loop, I want all the items associated with the id in the first table to stay on the same line:

mainrecord.id_incr | item1

1                          | flashlight, radio, pocketknife

2                          | phone, paintbrush

Link to comment
https://forums.phpfreaks.com/topic/220252-joining-tables/#findComment-1141427
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.