gpt4 book ai didi

mysql - 如何在codeigniter mysql中合并两个相同的行并显示不同的id行

转载 作者:行者123 更新时间:2023-11-29 11:35:07 27 4
gpt4 key购买 nike

我的 table 是这样的

 id ,  user_id , product_id , product_name , sub_name , status
1 , 1 , 001 , name1 , sub1, 1
2 , 1 , 001 , name1 , sub2, 1
3 , 1 , 002 , name2 , sub3, 1

我想在我的 View 页面中显示我的结果

 id , user_id , product_id , product_name , sub_name , status
1 , 1 , 001 , name1 , sub1, 1
3 , 1 , 002 , name2 , sub3, 1

我的Sql查询

$data['product'] = $this->db->group_by('product_id')
->where(array('user_id' => $id, 'product_id' => $product_id))->get('sr_shortserials')->result();

但我就是这样

 id , user_id , product_id , product_name , sub_name , status
3 , 1 , 002 , name2 , sub3, 1

这个可以,但我想要 id 1 和 3

最佳答案

我假设您想要显示表中具有最早的 sub_name 字段值的所有产品。

最好的方法是重组您的数据并将 sub_name 字段分离到其单独的表中,因为您当前的数据结构在正常形式阶梯上并不是太高。

但是查询的主要逻辑并不真正依赖于 sub_name 是否是不同的表。您需要获取每个产品的最小 id(如果 sub_names 位于不同的表中,则为最小 sub_name id)并连接回主表(单独的 sub_names 表)以获得正确的详细信息。

select t1.*
from yourtable t1
inner join
(select user_id, product_id, min(id) as minid
from yourtable
group by user_id, product_id) t2 on t1.id=t2.minid

关于mysql - 如何在codeigniter mysql中合并两个相同的行并显示不同的id行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com