简体   繁体   English

从另一个mysql表中获取值取决于另一个mysql表中的值

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

I have 2 tables. 我有2张桌子。 categories and shops. 类别和商店。 Table Shops have columns namely categories. 餐桌商店有列即类别。 categories have different ids of shops. 类别具有不同的商店ID。 Table categories have columns namely ids and parent. 表类别具有列,即ID和父级。 ids have shops ids and parent have parentid for eachshop. ID具有商店ID,父母具有eachshop的父母ID。

Now my task is to print the parent id of different ids of category column in shop table. 现在,我的任务是打印shop表中category列的不同id的父id。 Please help me 请帮我

category column contains values like "24,36,32" in one field and another field like "22,33,44". 类别列在一个字段中包含值“ 24,36,32”,在另一个字段中包含值“ 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 您应该使用join

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

Tutorial 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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