1

你好,我有两个表,PK---->InnoDB 引擎中的 FK 关系----> MySQL&PHP

关系是一个---->第一个表是“属性”和第二个表之间有很多关系

表是'propertyimages'。第一个表中的每一行都是唯一的,但在第二个表中

第一个表的每一行在第二个表中都有很多行我如何**选择唯一行

第一个表和第二个表中关于第一个表的所有信息是我的查询:

SELECT DISTINCT properties.PropertyName, 
                properties.PropertyStatus, 
                propertyimages.PropertyImageID, 
                propertyimages.ImagePath 
FROM properties 
   INNER JOIN propertyimages 
      ON properties.PropertyImageID=propertyimages.PropertyImageID 
     AND propertyimages.PropertyImageID=8;

它给出了结果:

PropertyName PropertyStatus Propertyid 属性图像路径
出租公寓 8 upload/hydrangeas.jpg
出租公寓 8 upload/jelsh.jpg
出租公寓 8 upload/penguins.jpg
出租公寓 8 upload/tulips.jpg

在这个结果中, PropertyNamePropertyStatus是重复的,但我想要一个

唯一的,因为它存储在第一个表中的propertyNamePropertyStatus

属于第一个表。PropertyidPropertyImagepath属于第二个表。

4

3 回答 3

0

不幸的是,连接将为表 2 中找到的每个元素创建一条记录,并在第一个表中具有匹配的父元素 (id 8)。

您可以在第一个表字段上使用 GROUP BY id 8,但您可能无法获得所需的所有结果,因为它只会获取第一张图像。

您能否重组您的流程,以便在涉及图像(及其显示)时,您只需运行一个查询即可获取与属性 8 相关的每个图像

您始终可以使用嵌套查询(假设您希望在页面上显示属性的图像等)

$query1 = mysql_query("SELECT Propertyid, PropertyName, PropertyStatus FROM properties WHERE (search criteria)");
if (mysql_num_rows($query1) > 0) {
    while ($q1Row = mysql_fetch_array($query1)) {

        // Insert property specific data here before you display the images
        echo $q1Row['PropertyName']."<br>\n";
        echo $q1Row['PropertyStatus']."<br>\n";

        $query2 = "SELECT PropertyImageID, ImagePath FROM propertyimages WHERE PropertyImageID='".$q1Row['Propertyid']."'"
            if (mysql_num_rows($query2) > 0) {
                while ($q2Row = mysql_fetch_array($query2)) {

                    // add code here to do whatever you want with the images
                    echo '<image src="'.$q2Row['ImagePath'].'"><br>\n';

                }
            }
        }
    }
}

了解您的数据库结构也将有所帮助..我想您会想要这样的东西

table 1 (properties)
  PropertyID (Primary Key)
  PropertyName
  PropertyStatus

table 2 (propertyImages)
  ImageID (Primary Key)
  PropertyID (many to one reference to PropertyID in table 1)
  ImagePath

我可能对 FK 方法有点忘记,所以如果这里也有适合我的课程,我很想听听你有什么意见。

于 2013-04-21T08:25:50.707 回答
0

你离解决方案只有一步之遥,你只需要从第二个表中选择,然后是第一个,这样你就可以得到所有匹配的结果

SELECT DISTINCT properties.PropertyName, 
            properties.PropertyStatus, 
            propertyimages.PropertyImageID, 
            propertyimages.ImagePath 
FROM propertyimages 
   INNER JOIN properties  
     ON propertyimages.PropertyImageID = properties.PropertyImageID
     AND propertyimages.PropertyImageID=8;
于 2013-04-21T08:42:40.957 回答
0

您可以使用GROUP_CONCAT来实现这一点。但我也认为,最好进行两个查询。没有错。

SELECT DISTINCT properties.PropertyName, 
                properties.PropertyStatus, 
                propertyimages.PropertyImageID, 
                GROUP_CONCAT(propertyimages.ImagePath) 
FROM properties 
   INNER JOIN propertyimages 
      ON properties.PropertyImageID=propertyimages.PropertyImageID 
      AND propertyimages.PropertyImageID=8;
GROUP BY propertyimages.PropertyImageID
于 2013-04-21T08:55:44.283 回答