从两个表中选择mysqli

时间:2013-12-02 11:50:42

标签: php mysqli

我有一个页面,其中包含项目列表,我在数据库表上有两个表,一个内容项目的信息,表两个内容每个项目的图像image_id是表上的外键二 现在我需要做的是选择所有信息并从表2中取image_id一张图像,使其成为主项目图像。

这是我的代码

<?php
    $getPro="SELECT * FROM project_info, projects_images WHERE(projects_images.image_id=project_info.id)";
    $QgetPro=$db->query($getPro)or die($db->error);
?>

<div class="body2">
  <div class="main">
    <section id="content2">
      <div class="wrapper">
        <article class="col1_fx">
          <div class="pad2">
            <h2 class="pad_bot1 pad_top1">Projects</h2>
            <div class="wrapper">
              <div style="float:left">
                <?php
              while($p=$QgetPro->fetch_object()){
              ?>
                <div class="main_pro_holder">
                  <div class="pro_imageHolder"><a href="projects_details.html"><img src="images/<?php echo $p->image_1; ?>" class="imageBorder" width="260" height="150"  alt=""/></a></div>
                  <div class="pro_label"><strong class="color1"><a href="projects_details.html"><?php echo $p->pro_title; ?></a></strong></div>
                  <div class="pro_sammry">
                    <table border="0" width="260">
                      <tr>
                        <td><strong class="color_001">Bilder</strong></td>
                        <td>El Arabia</td>
                      </tr>
                      <tr>
                        <td><strong class="color_001">Region</strong></td>
                        <td><?php echo $p->pro_address ?></td>
                      </tr>
                      <tr>
                        <td><strong class="color_001">Tracts</strong></td>
                        <td><?php echo $p->pro_area_from ?> to <?php echo $p->pro_area_to ?></td>
                      </tr>
                      <tr>
                        <td><strong class="color_001">Plans</strong></td>
                        <td><a href="#">See Plans</a></td>
                      </tr>
                      <tr>
                        <td colspan="2" align="right"><a href="index.php?pid=2&proid=2&prodeid=<?php echo $p->id; ?>" class="button" style="margin-top:5px;">Details</a></td>
                      </tr>
                    </table>
                  </div>
                </div>
                <?php } ?>
              </div>
            </div>
          </div>
        </article>
      </div>
    </section>
  </div>
</div>
<!-- / content -->

用于此工作的正确MySQLi语法是什么

4 个答案:

答案 0 :(得分:2)

$getPro="SELECT project_info.*, projects_images.* FROM project_info INNER JOIN projects_images ON projects_images.image_id=project_info.id";

编辑:

您需要使用 GROUP BY

$getPro="SELECT project_info.*, projects_images.* FROM project_info INNER JOIN projects_images ON projects_images.image_id=project_info.id GROUP BY projects_images.image_id";

答案 1 :(得分:1)

与我合作的MySQL就是这个

$getPro="SELECT DISTINCT project_info.*, projects_images.image_id,image FROM project_info INNER JOIN projects_images ON projects_images.image_id=project_info.id GROUP BY projects_images.image_id";  

我刚刚添加了projects_images.image_id,image来获取信息表中的图片ID和图片表中的图片。 谢谢大家

答案 2 :(得分:0)

使用加入查询,

 select * from project_info a join projects_images b on b.image_id = a.image_id

答案 3 :(得分:0)

$getPro = "SELECT DISTINCT project_info.*,
            projects_images.imageName
FROM project_info
JOIN projects_images on(projects_images.image_id=project_info.id)

请尝试这个。我认为这将解决您的问题。

此处distinct将立即返回一个组合。

其次,您可以使用GROUPBY子句

$getPro = "SELECT project_info.*,
       projects_images.*
FROM project_info
JOIN projects_images on(projects_images.image_id=project_info.id)
GROUP BY project_info.id";