0

Hi i want to acheive the following results for the purpose of a search script in php i have 2 tables like this

Table 1 :

-----------------------------
id  l name l url l image url  l
-------------------------------

Table 2 :
-----------------------------------
id  l tableoneid l desc l content  l
------------------------------------

Note : (Table 1 --> id) = (Table 2 ---> tableoneid)

what i want to acheive is to get a MYSQL query that search :

  1. Step 1 : The table 2 column [content] when i get the result i need their [tableoneid] values,
  2. Step 2 : Next i want to use that in order to search the Table 1 column 1
  3. Step 3 : The final results would be the corresponding [image url] column of the Step 2 results

how can i acheive that with php/mysql

thank you very much !

MAV
  • 29
  • 8
  • Possible duplicate of [Difference between INNER and OUTER joins](http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins) – kero Apr 20 '16 at 23:08

3 Answers3

0

Something like this:

SELECT * FROM <Table 1>
WHERE id IN (
    SELECT DISTINCT(tableoneid) FROM <Table 2>
)

Change <Table 1> and <Table 2> with your actual table names.

BeS
  • 817
  • 4
  • 9
0

This is one of the primary uses of JOINs.

SELECT t1.`image url`
FROM `Table 2` AS t2
INNER JOIN `Table 1` AS t1 ON t2.tableoneid = t1.id
WHERE [some condition involving t1.content]
;

I would strongly suggest not having any sort of non-alpha characters (other than _ in your table and field names.)

Uueerdo
  • 15,723
  • 1
  • 16
  • 21
0
$query = "select
  t1.`image url`
from
  `Table 1` as t1
join
  `Table 2` as t2 on t1.id = t2.tableoneid
where
  t2.content = :table2ContentSearch";

$statement = $dbh->prepare($query);
$searchItem = "what you are searching for";
$statement->bindParam(':table2ContentSearch', $searchItem);
if($statement->execute()){
   while($row = $statement->fetch()){
     print_r($row); 
   }
}

you should also read up on sql joins. https://en.wikipedia.org/wiki/Join_%28SQL%29.

decapo
  • 789
  • 3
  • 8
  • 25