简体   繁体   中英

Joining 2 statements to pull data from different tables

I have this code:

<?php
$data = mysql_query("SELECT * FROM repin WHERE new_pin_id LIKE ".$pinDetails->id) or         die(mysql_error()); 
while($info = mysql_fetch_array( $data ))
{
Print "".$info['from_pin_id'].",".$info['new_pin_id']."";
} 
?>

Obtained thanks to this article: Check field for identical number

I'm trying to use the detail I pulled: ".$info['from_pin_id']." to get data from another table. I'm looking for the best way to do this.

I thought about making it a variable and then running a second statement within the same <?php?> which would look something like this:

Print "".$info['from_pin_id'].",".$info['new_pin_id']."";
} 
$newdata = "".$info['from_pin_id']."";
// new statement here.
?>

But 1. it won't work and 2. it looks messy.

What is the best way to achieve it?

FYI, what I need to do is use ".$info['from_pin_id']." to match a field in another table where the data is the same ID, then pull more info based on the match.

Use the following query:

"SELECT *
FROM repin r
LEFT JOIN otherTable o
ON o.someColumn = r.from_pin_id
WHERE r.new_pin_id LIKE '".$pinDetails->id."'"

Also, the argument to LIKE must be a string; you need to put quotes around it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM