简体   繁体   English

sql / php从多个表条目中获取最小值和最大值

[英]sql/php to grab the minimum & maximum from multiple table entries

I hope the below makes sense... 我希望以下内容是合理的...

I'm grabbing all of the children records using the following (another script does the getChildRecords ): 我使用以下命令抓取所有子记录(另一个脚本执行getChildRecords ):

<?php $children = $this->item->getChildRecords(); ?>

I am then doing a foreach to grab all of the children house ID's: 然后,我正在做一个foreach来获取所有儿童房ID:

<?php
foreach ( $children as $i => $id )
  {
    $house = JTable::getInstance('house', 'MyTable');
    $house->load($id);
?>

Then I am pulling back the number of rooms for each child house entry: 然后,我为每个儿童房屋入口拉回房间数量:

<div><?php $house->rooms; ?></div>

And finishing off my foreach: 完成我的foreach:

<?php } ?>

The children table may contain the following room entries: 子表可能包含以下房间条目:

ID - 22 Rooms - 1 | ID -22 客房 -1 | ID - 22 Rooms - 5 | ID -22 客房 -5 | ID - 22 Rooms - 2 | ID -22 客房 -2 | ID - 22 Rooms - 3 ID -22 客房 -3

Question: 题:

What I want to show is the Min to Max number of rooms from all of the child entries. 我要显示的是所有子条目中的最小到最大房间数。

ie 1-5 即1-5

Do you even need the single children entries on that page? 您甚至需要该页面上的单身子女条目吗? Or is it an overview? 还是概述? If you don't need the single entries you could create a function like 如果您不需要单个条目,则可以创建一个函数,例如

public function getMinAndMaxRooms() {
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    $result mysqli_query($con,"SELECT id, MIN(rooms), MAX(rooms) FROM rooms GROUP BY id");
    if ($result !== false) {
        return $result->fetch_all(MYSQLI_NUM);
    }
    return array();
}

And in your template cycle through it: 在模板中循环:

<?php foreach ($this->item->getChildRecords() as $child) : ?>
<?php echo $child[0) . ': ' . $child[1] . '-' - $child[2] ?>
<?php endforeach; ?>

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

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