Heim >Datenbank >MySQL-Tutorial >Mysql查询中供应商本身的任意产品的使用
以下的文章主要讲述的是Mysql查询中所有的供应商与其本身的任意一个产品的使用的方案描述,本文主要讲述的是Group by的具体使用方案,因一网友提出的需求,其主要内偶然那个如下所示: 有供应商表a,商品表b 以下简称a,b,a与b是一对多的关系(一条供应商对应
以下的文章主要讲述的是Mysql查询中所有的供应商与其本身的任意一个产品的使用的方案描述,本文主要讲述的是Group by的具体使用方案,因一网友提出的需求,其主要内偶然那个如下所示:
有供应商表a,商品表b 以下简称a,b,a与b是一对多的关系(一条供应商对应多条商品) ,现在想一对一的提取 ,即所有的供应商都提取出来,但是每条供应商只提取一条商品记录对应起来就行了。
我提供的测试代码如下:
<ol class="dp-xml"> <li class="alt"><span><span>view plaincopy to clipboardprint? </span></span></li> <li><span>CREATE TABLE `t_supplier` ( </span></li> <li class="alt"><span>`id` int(11) NOT NULL, </span></li> <li><span>`name` varchar(50) NOT NULL, </span></li> <li class="alt"><span>PRIMARY KEY (`id`) </span></li> <li> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">InnoDB</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>; </span> </li> <li class="alt"><span>CREATE TABLE `t_goods` ( </span></li> <li><span>`Id` int(11) NOT NULL AUTO_INCREMENT, </span></li> <li class="alt"><span>`Supplier_Id` int(11) NOT NULL, </span></li> <li><span>`Name` varchar(50) DEFAULT NULL, </span></li> <li class="alt"><span>PRIMARY KEY (`Id`), </span></li> <li><span>KEY `Supplier_Id` (`Supplier_Id`), </span></li> <li class="alt"><span>CONSTRAINT `t_goods_ibfk_1` FOREIGN KEY (`Supplier_Id`) <br>REFERENCES `t_supplier` (`id`) </span></li> <li> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">InnoDB</span><span> </span><span class="attribute">AUTO_INCREMENT</span><span>=</span><span class="attribute-value">5</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>; </span> </li> <li class="alt"><span>insert into t_Supplier values (1,'天津供应商'),(2,'北京供应商'); </span></li> <li><span>insert into t_Goods values(1,1,'天津产品1'),<br>(2,1,'天津产品2'),(3,2,'北京产品1'),(4,2,'北京产品2'); </span></li> </ol>
Mysql查询语句,注意group 的用法
<ol class="dp-xml"> <li class="alt"><span><span>select * from t_supplier s </span></span></li> <li> <span>left join t_goods g on </span><span class="attribute">g.supplier_id</span><span>=s.id </span> </li> <li class="alt"><span>group by s.id </span></li> </ol>
Mysql查询结果 所有的供应商,和其一个产品
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TABLE `t_supplier` ( </span></span></li> <li><span>`id` int(11) NOT NULL, </span></li> <li class="alt"><span>`name` varchar(50) NOT NULL, </span></li> <li><span>PRIMARY KEY (`id`) </span></li> <li class="alt"> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">InnoDB</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>; </span> </li> <li><span>CREATE TABLE `t_goods` ( </span></li> <li class="alt"><span>`Id` int(11) NOT NULL AUTO_INCREMENT, </span></li> <li><span>`Supplier_Id` int(11) NOT NULL, </span></li> <li class="alt"><span>`Name` varchar(50) DEFAULT NULL, </span></li> <li><span>PRIMARY KEY (`Id`), </span></li> <li class="alt"><span>KEY `Supplier_Id` (`Supplier_Id`), </span></li> <li><span>CONSTRAINT `t_goods_ibfk_1` FOREIGN KEY (`Supplier_Id`) REFERENCES `t_supplier` (`id`) </span></li> <li class="alt"> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">InnoDB</span><span> </span><span class="attribute">AUTO_INCREMENT</span><span>=</span><span class="attribute-value">5</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>; </span> </li> <li><span>insert into t_Supplier values (1,'天津供应商'),(2,'北京供应商'); </span></li> <li class="alt"><span>insert into t_Goods values(1,1,'天津产品1'),(2,1,'天津产品2'),(3,2,'北京产品1'),(4,2,'北京产品2'); </span></li> </ol>
查询语句,注意group 的用法
<ol class="dp-xml"> <li class="alt"><span><span>select * from t_supplier s </span></span></li> <li> <span>left join t_goods g on </span><span class="attribute">g.supplier_id</span><span>=s.id </span> </li> <li class="alt"><span>group by s.id </span></li> </ol>
Mysql查询结果,所有的供应商,和其一个产品在Mysql里,非Group 和聚合的字段是可以出现select里面的,系统会自动选择一个数据。在别的数据库里是不允许这么使用的 。