简体   繁体   English

如何从php的mysql的每个表的特定行中检索数据?

[英]how to retrieve data from particular rows of every table of mysql in php?

I have three tables as h_basic, h_amenities and h_images with common column u_name. 我有三个表,分别为h_basic,h_amenities和h_images,且具有公用列u_name。 there are two php pages named product-list and product-review. 有两个php页面,分别命名为product-list和product-review。 I want to fetch the details of a particular product on product-review page when I click on the button of that product on product-list page. 当我在“产品列表”页面上单击某个产品的按钮时,我想在“产品评论”页面上获取该产品的详细信息。

I have the code that retrieves a single row from a single table, but i want to retrieve particular rows of all three tables and i also want to take u_name as primary not id since id is varying in all the tables. 我有从单个表检索单个行的代码,但是我想检索所有三个表的特定行,并且我还想将u_name用作主要而不是id,因为id在所有表中都不同。

 <?php $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0"; $sql = ("SELECT h_basic.*, h_images.* FROM h_basic JOIN h_images WHERE h_basic.id = h_images.id"); $result = mysql_query("$sql"); while ($row = mysql_fetch_array($result)) { echo $row['h_name']; echo $row['h_state']; ?> <img src="images/<?php echo $row['h_image01'];?>"><img src="images/<?php echo $row['h_image02'];?>"><img src="images/<?php echo $row['h_image03'];?>"><img src="images/<?php echo $row['h_image04'];?>"> <?php }?> 

Table 1: 表格1:

在此处输入图片说明

Table 2: 表2:

在此处输入图片说明

Just use whatever fields do match as the ON condition of a JOIN 只需使用匹配的任何字段作为JOINON条件

SELECT B.*, I.*, A.*
FROM h_basic B
LEFT JOIN h_images I ON B.u_name = I.u_name
LEFT JOIN h_amenities A IN B.u_name = A.u_name

You would need to add a WHERE clause to get a specific name 您将需要添加WHERE子句以获取特定名称

Notice this will go faster if you have an index on each table for the joining fields 请注意,如果在每个表上都有用于联接字段的索引,则这样做会更快

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

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