繁体   English   中英

如何查询2个表和多个列?

[英]How Can I Query 2 Tables And Multiple Columns?

我已经对此进行了研究,到目前为止,我的所有尝试都没有完成。 我试图在处理多个表的PHP脚本中执行mysql查询。

表格如下所示:

表格1

名称

表2

产品名称)

库存

猫ID

产品编号

表3

product_url

“名称”(表1)必须与“产品”(表2)一样理智。 接下来,“库存”(表2)必须为=到“ Y”。 最后,“ CatID”必须为=“ 2”。

我的尝试看起来像这样:

SELECT 1.name, 2.Product, 2.Inventory, 2.CatID
FROM table1 1, table2 2 
WHERE 2.Inventory = 'Y'
  AND 1.name = 2.Product
  AND 2.CatID = '2'

从结果中,我希望从表中获取更多信息,例如产品描述等,这些信息将在table1和table2中...我之前从未加入或查询过2个(或更多)表。 任何帮助将不胜感激。

尝试这个:

SELECT table1.Name, table2.Product, tabl2.Inventory, table2.CatID
FROM table1 INNER JOIN table2
ON table1.Name = table2.Product 
WHERE table2.CatID = '2'
SELECT t1.name, t2.Product, t2.Inventory, t2.CatID, t2.ProductID
FROM table1 t1
INNER JOIN table2 t2 ON t2.Product = t1.name
WHERE t2.Inventory = 'Y' AND t2.CatID = 2

很抱歉,您必须使用的数据库设计得很差。 如果我给您的查询不起作用,请确保表中的数据确实符合您要查找的条件。

还要记住,当您在PHP中访问这些字段时,大小写很重要。 您需要执行以下操作:

<?php

$q = QUERY FROM ABOVE
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)) {
    $name = $row["name"];
    $product = $row["Product"];
    $inventory = $row["Inventory"];
    $catid = $row["CatID"];
    $productid = $row["ProductID"];
}

?>

暂无
暂无

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

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