Mysql codeigniter从多个表中选择最低和最高产品价格
我需要从产品、价格中选择所有(*)行。。但是我需要从价格中选择最小和最大价格。我已经读过如何做到这一点,但如何在内部连接中做到这一点 这是我的build_query()函数:Mysql codeigniter从多个表中选择最低和最高产品价格,mysql,codeigniter,Mysql,Codeigniter,我需要从产品、价格中选择所有(*)行。。但是我需要从价格中选择最小和最大价格。我已经读过如何做到这一点,但如何在内部连接中做到这一点 这是我的build_query()函数: //build sql query string public function build_query($type = "active") { $select = "products.*, (SELECT CONCAT(price) FROM prices WHE
//build sql query string
public function build_query($type = "active")
{
$select = "products.*,
(SELECT CONCAT(price) FROM prices WHERE products.id = prices.product_id ORDER BY created_at DESC LIMIT 1) AS price;}
public function get_shops_products_maxmin_price($parent_id)
{
$this->build_query();
$this->db->select('MAX(price) as p_max, MIN(price) p_min ');
$this->db->where('products.parent_id', clean_number($parent_id));
return $this->db->get('products')->result();
}
这是我的products\u maxmin\u price()函数:
//build sql query string
public function build_query($type = "active")
{
$select = "products.*,
(SELECT CONCAT(price) FROM prices WHERE products.id = prices.product_id ORDER BY created_at DESC LIMIT 1) AS price;}
public function get_shops_products_maxmin_price($parent_id)
{
$this->build_query();
$this->db->select('MAX(price) as p_max, MIN(price) p_min ');
$this->db->where('products.parent_id', clean_number($parent_id));
return $this->db->get('products')->result();
}
这是我的观点:
<?php foreach ($shops_products_maxmin_price as $item): ?>
<?php echo $item->p_min; ?>
<?php endforeach; endif; ?>
<?php foreach ($shops_products_maxmin_price as $item): ?>
<?php echo $item->p_max; ?>
<?php endforeach; endif; ?>
尝试以下查询:
SELECT MIN(`prices`.`price`) as `p_min`,
MAX(`prices`.`price`) as `p_max`,
`products`.*
FROM `prices`
JOIN `products`
ON `prices`.`product_id` = `products`.`id`
WHERE `products`.`parent_id` = `<PARENT_ID>`
GROUP BY `prices`.`product_id`;
为什么要将价格存储在一个单独的表中?因为我们需要价格历史记录,一个产品在同一日期具有某个价格如果是我,我会从原始查询开始Ror:Message:Undefined property:stdClass:$p_Mint这只是一个SQL查询!不要尝试用PHP运行!我试图帮助您找到正确的问题查询。您需要将此查询转换为您的代码,因为我(我们)不知道您的环境详细信息我是一名初学者,无法在codeigniter中使用您的查询,如果您可以,请在我的函数中应用,然后您必须先学习基础知识,然后再处理更严重的项目错误编号:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解在
prices
products\u id
=products
id
,其中products
par'上使用的正确语法,在第2行选择MIN(
pricesprice)作为
p\MIN,MAX>)(
价格
价格)作为
p\u max,
products*加入
products`ONprices
products
id
其中products
父\u id
=1组BY价格产品
product id
订购人价格在
描述处创建的产品