Jump to content

Re: mysql select question


giraffemedia

Recommended Posts

Hello

 

is it possible to select rows from one table where the data contained in a column is equal to that of another table, or is it best to use a variable as one of the references?

 

i.e

SELECT * FROM test1 WHERE test1.column1 = test2.column1

 

or

// find all the id's in the table
$query = ("SELECT id FROM test1 ORDER BY id ASC") or die(0);

$query_result = mysql_query ($query);

if (!$query_result) { 
echo ("<strong>Problem because:</strong> " . mysql_error());
}

// while loop to run through all the id's

while ($row = mysql_fetch_array($query_result, MYSQL_ASSOC)) {
$id = $row['id'];
}

// find all results from 
$results = SELECT * FROM test2 WHERE test2.column1 = $id

$query_results = mysql_query ($results);

if (!$query_results) { 
echo ("<strong>Problem because:</strong> " . mysql_error());
}

// then loop and echo the results

while ($row2 = mysql_fetch_array($query_results, MYSQL_ASSOC)) {
$id = $row2['id'];

echo $id;
}

 

Thanks

 

James

Link to comment
Share on other sites

The ID on the test1 is the primary key. The column on the other table I am trying to match it up with also has the ID as it's primary key but I don't want to join the table by that column.

 

Can you only do inner joins by using the primary keys from both columns?

 

I'm also reading up on the join process as we speak!  ;)

 

Regards

 

James

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.