duanjiaren8188 2013-11-27 12:47
浏览 59
已采纳

合并两个表并回显结果(PHP MYSQL)

I have two tables; 'client' and 'sys_notes' in the same DB. (SELECT client.*, sys_notes.* FROM client LEFT JOIN sys_notes ON sys_notes.client_id = client.id ORDER BY create_date DESC)

I need to combine the two tables echo in the same <div>content</div>, like this:

echo all from client = first.name + last.name (e.g. Scott)
echo all from client = role
echo all from client = whatever else
echo all from sys_notes sent by Robert Scott = note1, note2, note3, note(etc.)...
echo all from client = whatever else
echo all from client = whatever else

example output:


<div class="content">

Robert Scott
Supervisor
Robert works at the order office at the firm.
Robert´s notes:

  • today: (Note from sys_notes table)
  • yesterday: (Note from sys_notes table)
  • 19.oct 2013: (Note from sys_notes table)

Robert´s Phone: 000 000 000
Robert´s E-mail: etc.
Other information about Robert: ............

</div>

<div class="content">

Lisa Johansson
Supervisor
Lisa works at the marketing dep.
Lisas´s notes:

  • today: (Note from sys_notes table)
  • 12.may 2013: (Note from sys_notes table)

Lisa´s Phone: 000 000 000
Lisa´s E-mail: etc.
Other information about Lisa: ............

</div>

When i echo results today Robert is being "echoed" as many times he has notes,
with one note in each <div class="content">. (In example over Robert has 3 notes & 3 different Robert-div is echoed.)

Please help.

--- UPDATE ---

-- Table structure for table `sys_notes`

CREATE TABLE IF NOT EXISTS `sys_notes` (
  `note_id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) NOT NULL,
  `note_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `owning_user` varchar(50) NOT NULL,
  `note` varchar(500) NOT NULL,
  PRIMARY KEY (`note_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;

-- Table structure for table `client`

CREATE TABLE IF NOT EXISTS `client` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `create_date` datetime NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `email` varchar(50) NOT NULL,
  `issue` varchar(50) NOT NULL,
  `other` longtext NOT NULL,
  `owning_user` int(11) NOT NULL,
  `status` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1081 ;
  • 写回答

2条回答 默认 最新

  • dpxyfa4718 2013-11-27 12:50
    关注

    use:

    SELECT DISTINCT
    

    instead of:

    SELECT
    

    or GROUP BY statement.

    -- EDIT --

    Example echo:

    $result = $db->query("SELECT client.*, GROUP_CONCAT(sys_notes.note SEPARATOR "|") as note, sys_notes.* FROM client LEFT JOIN sys_notes ON sys_notes.client_id = client.id GROUP BY sys_notes.client_id ORDER BY create_date DESC");
    
    ob_start();
    ?>
    
    <div class="content">
    
    <?php
    foreach($result as $row) {
    
        sprintf("<p>%s %s<br />%s<br />%s</p> %s <ul>", $row['firstname'], $row['lastname'], $row['role'], $row['desc'](example));
    
        foreach(explode("|", $row['note']) as $note) {
            sprintf("<li>%s</li>", $note);
        }
    
        sprintf("</ul>");
    }
    ?>
    </div>
    <?
    
    $result = ob_end_clean();
    
    echo $result;
    

    -- FINAL UPDATE --

    <?php
    
    try {
        $db = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    
    $result = $db->query('SELECT client . * , GROUP_CONCAT( sys_notes.note SEPARATOR  "|" ) AS n, sys_notes.* 
    FROM client
    LEFT JOIN sys_notes ON sys_notes.client_id = client.id
    GROUP BY sys_notes.client_id
    ORDER BY create_date DESC');
    
    ob_start();
    ?>
    
    <div class="content">
    
    <?php
    
    foreach($result as $row) {
    
        printf("<p>%s %s<br />%s</p> %s <ul>", $row['first_name'], $row['last_name'], $row['issue'], $row['other']);
    
        foreach(explode("|", $row['n']) as $note) {
            printf("<li>%s</li>", $note);
        }
    
        printf("</ul>");
    }
    ?>
    </div>
    <?php
    
    ob_end_flush();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c