您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Mysql查询中供应商本身的任意产品的使用

2025/11/5 18:13:56发布15次查看
以下的文章主要讲述的是mysql查询中所有的供应商与其本身的任意一个产品的使用的方案描述,本文主要讲述的是group by的具体使用方案,因一网友提出的需求,其主要内偶然那个如下所示: 有供应商表a,商品表b 以下简称a,b,a与b是一对多的关系(一条供应商对应
以下的文章主要讲述的是mysql查询中所有的供应商与其本身的任意一个产品的使用的方案描述,本文主要讲述的是group by的具体使用方案,因一网友提出的需求,其主要内偶然那个如下所示:
有供应商表a,商品表b 以下简称a,b,a与b是一对多的关系(一条供应商对应多条商品) ,现在想一对一的提取 ,即所有的供应商都提取出来,但是每条供应商只提取一条商品记录对应起来就行了。
我提供的测试代码如下:
view plaincopy to clipboardprint?  create table `t_supplier` (   `id` int(11) not null,   `name` varchar(50) not null,   primary key (`id`)   ) engine=innodb default charset=utf8;   create table `t_goods` (   `id` int(11) not null auto_increment,   `supplier_id` int(11) not null,   `name` varchar(50) default null,   primary key (`id`),   key `supplier_id` (`supplier_id`),   constraint `t_goods_ibfk_1` foreign key (`supplier_id`) 
references `t_supplier` (`id`)   ) engine=innodb auto_increment=5 default charset=utf8;   insert into t_supplier values (1,'天津供应商'),(2,'北京供应商');   insert into t_goods values(1,1,'天津产品1'),
(2,1,'天津产品2'),(3,2,'北京产品1'),(4,2,'北京产品2');  
mysql查询语句,注意group 的用法
select * from t_supplier s   left join t_goods g on g.supplier_id=s.id   group by s.id  
mysql查询结果 所有的供应商,和其一个产品
create table `t_supplier` (  `id` int(11) not null,  `name` varchar(50) not null,  primary key (`id`)  ) engine=innodb default charset=utf8;  create table `t_goods` (  `id` int(11) not null auto_increment,  `supplier_id` int(11) not null,  `name` varchar(50) default null,  primary key (`id`),  key `supplier_id` (`supplier_id`),  constraint `t_goods_ibfk_1` foreign key (`supplier_id`) references `t_supplier` (`id`)  ) engine=innodb auto_increment=5 default charset=utf8;  insert into t_supplier values (1,'天津供应商'),(2,'北京供应商');  insert into t_goods values(1,1,'天津产品1'),(2,1,'天津产品2'),(3,2,'北京产品1'),(4,2,'北京产品2'); 
查询语句,注意group 的用法
select * from t_supplier s  left join t_goods g on g.supplier_id=s.id  group by s.id 
mysql查询结果,所有的供应商,和其一个产品在mysql里,非group 和聚合的字段是可以出现select里面的,系统会自动选择一个数据。在别的数据库里是不允许这么使用的 。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product