简体   繁体   中英

getting a value from one mysql table depending on the value of another mysql table

I have 2 tables. categories and shops. Table Shops have columns namely categories. categories have different ids of shops. Table categories have columns namely ids and parent. ids have shops ids and parent have parentid for eachshop.

Now my task is to print the parent id of different ids of category column in shop table. Please help me

category column contains values like "24,36,32" in one field and another field like "22,33,44".

<?php
$con = mysql_connect("localhost", "abc", "1234");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("db",$con);
$sql = "SELECT categories from shops";
$array = mysql_query($sql,$con);
while($row=mysql_fetch_array($array)){
foreach($row as $value){
    $query="SELECT parent FROM categories where categories.id=$value.'<br/>'.";
    echo $query;
    }
    }
mysql_close($con);
?>

You should use join

SELECT A.ForeignId, B.Id
FROM tablea A
LEFT JOIN tableb B ON B.Id=A.ForeignId

Tutorial

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