【问题标题】:SQL Join doesn't show up correctlySQL 连接未正确显示
【发布时间】:2013-06-12 06:00:00
【问题描述】:

我正在尝试寻找解决方案,我正在尝试从使用相同 ID 的不同表中获取数据。这是我的代码

 "SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id";

我需要创建部分,然后用一些内容填充它们,表 menucat 是包含部分的父表,vmenutab 是包含内容的子表。 但我有一个问题,它没有正确显示。应该是这样的:

第 1 节

  • 链接1
  • 链接2

但它显示如下:

第 1 节

  • 链接1

第 1 节

  • 链接2

我一直在使用搜索,GROUP BY 和 DISTINCT 没有用。

表格:

菜单: cat_id menu_cat_est menu_cat_ru menu_cat_cn

虚拟菜单: id(唯一链接 id) menu_id(到相关部分的 id) menu_name_est menu_name_ru menu_name_en

menu_cat_xxx 和 menu_name_xxx 是不同的语言数据

PHP 代码

<?php
$target = "SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id";



$mq     = mysql_query($target);
while ($row = mysql_fetch_array($mq)) {
    $menu_cat_est   = $row['menu_cat_est'];
    $menu_cat_ru    = $row['menu_cat_ru'];
    $menu_cat_en    = $row['menu_cat_en'];

    $menu_name_est   = $row['menu_name_est'];
    $menu_name_ru    = $row['menu_name_ru'];
    $menu_name_en    = $row['menu_name_en'];


    $cat_id         = $row['cat_id'];
    $id         = $row['id'];



    echo '<tr>
    <td>' . $menu_cat_est . ''.$menu_name_est.'</td>
    <td>' . $menu_cat_ru . ''.$menu_name_ru.'</td>
    <td>' . $menu_cat_en . ''.$menu_name_en.'</td>
    <td scope="col"><center><a href="content.php?veditcat=' . $cat_id . '"><img src="style/stylesheet/images/edit.png"></a></center></td>
    <td scope="col"><center><a href="content.php?vdelcat=' . $cat_id . '"><img src="style/stylesheet/images/delete.png"></a></center></td>
    <td scope="col"><center><a href="content.php?vadd=' . $cat_id . '">Добавить</a></center></td>
  </tr>';




}

?>

对不起我的英语。最好的问候。

编辑:添加了整个 PHP 代码。 编辑 #2:添加表格行。

【问题讨论】:

  • 内连接呢?
  • 你能在循环返回的行的地方发布你的 php 代码吗?
  • 在 PHP 中你使用 FULL JOIN 代替 LEFT
  • 如果我将 LEFT JOIN 替换为 FULL JOIN,则会显示错误:mysql_fetch_array() 需要参数
  • 在我发布我的代码之前,' . $menu_cat_xxx . ''.$menu_name_xxx.' 有点令人困惑。 $menu_cat_xxx 有时为空白

标签: php mysql sql database join


【解决方案1】:

一个例子,因为我不知道你的数据库数据。还可以按猫 ID 对 db 进行排序

' . $menu_cat_xxx . ''.$menu_name_xxx.' 有点混乱

$target = "SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id" order by menucat.cat_id;

$mq     = mysql_query($target);
while ($row = mysql_fetch_array($mq)) {
    $menu_cat_est   = $row['menu_cat_est'];
    $menu_cat_ru    = $row['menu_cat_ru'];
    $menu_cat_en    = $row['menu_cat_en'];

    $menu_name_est   = $row['menu_name_est'];
    $menu_name_ru    = $row['menu_name_ru'];
    $menu_name_en    = $row['menu_name_en'];

    $cat_id         = $row['cat_id'];
    $id         = $row['id'];



if(!$nocat[$cat_id]){ $nocat[$cat_id] = $cat_id; $section='<tr><td colspan="6"> section' . $cat_id . '</td></tr>
';}else{$section='';} // customise as i need sleep

echo $section.
'<tr>
<td>' . $menu_cat_est . ''.$menu_name_est.'</td>
<td>' . $menu_cat_ru . ''.$menu_name_ru.'</td>
<td>' . $menu_cat_en . ''.$menu_name_en.'</td>
<td scope="col"><center><a href="content.php?veditcat=' . $cat_id . '"><img src="style/stylesheet/images/edit.png"></a></center></td>
<td scope="col"><center><a href="content.php?vdelcat=' . $cat_id . '"><img src="style/stylesheet/images/delete.png"></a></center></td>
<td scope="col"><center><a href="content.php?vadd=' . $cat_id . '">Добавить</a></center></td>

';

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多