php mysqli 查询个门店数据(面向对象)

---恢复内容开始---

最近有一张如下的表,由于图片比较大,我改写为sql,如下所示:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `institution`
-- ----------------------------
DROP TABLE IF EXISTS `institution`;
CREATE TABLE `institution` (
`institution_id` smallint(10) NOT NULL AUTO_INCREMENT COMMENT '机构id',
`prov` varchar(18) NOT NULL DEFAULT '' COMMENT '省',
`city` varchar(18) NOT NULL DEFAULT '' COMMENT '市',
`dist` varchar(18) NOT NULL DEFAULT '' COMMENT '区县',
`name` varchar(30) NOT NULL DEFAULT '' COMMENT '机构名称',
`berth` int(11) NOT NULL COMMENT '床位',
`institution_type` tinyint(1) NOT NULL COMMENT '机构类型(0总公司、1分公司、2机构,3营销公司)',
`institution_type1` tinyint(1) NOT NULL DEFAULT '0' COMMENT '区分公司,营销中心;1营销中心',
`manager` varchar(20) NOT NULL COMMENT '负责人',
`tel` varchar(11) NOT NULL COMMENT '联系电话',
`tel1` varchar(11) NOT NULL DEFAULT '' COMMENT '门店电话',
`addr` varchar(200) NOT NULL DEFAULT '' COMMENT '机构地址',
`level` tinyint(4) NOT NULL DEFAULT '0' COMMENT '城市级别1-5级',
`level1` int(10) NOT NULL DEFAULT '0' COMMENT '消费等级 金额',
`location` varchar(100) NOT NULL DEFAULT '',
`area_code` varchar(100) NOT NULL COMMENT '区号',
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '营业状态 0新建; 1营业;2歇业',
PRIMARY KEY (`institution_id`)
) ENGINE=InnoDB AUTO_INCREMENT=290 DEFAULT CHARSET=utf8 COMMENT='机构基础信息表';

INSERT INTO `institution` VALUES ('23', '北京市', '北京市', '丰台区', '北京巨龙', '20', '0', '0', '苏建华', '13522279999', '', '北京市丰台区东铁营萎子坑138号嘉诚商务中心', '0', '0', '', '', '0');
INSERT INTO `institution` VALUES ('103', '北京市', '北京市', '丰台区', '北京痘庄', '0', '1', '0', '林飞虎', '18675238181', '', '丰台嘉诚商务中心', '0', '0', '', '', '0');

需要查询出来的结果要求是,把表内容按照省市区门店的方式列出来,并且要求:北京——丰台——某某门店,一对多的关系,不能出现两个北京丰台。

查询代码如下:


<?php

header("content-type:text/html;charset=utf-8");
$mysqli = new mysqli('localhost','root','','test');
//设置 查询为中文格式
$mysqli->query("set names utf8");
//查询是否成功
if(mysqli_connect_errno()){
die('Unable to connect!').mysqli_connect_errno();
}

$sql="select prov,group_concat(city separator '*')as citys,group_concat(dists separator '*')as dist,group_concat(name separator '*')as names,group_concat(institution_id)as institution from (select prov,city,group_concat(dist separator '#') as dists,group_concat(names separator '#')as name,group_concat(institution separator '#') as institution_id from (select prov,city,dist,group_concat(name) as names,group_concat(institution_id) as institution from `institution` group by prov,city,dist)as tmp1 group by prov,city)as tmp2 group by prov";
$result=$mysqli->query($sql);
while ($row=$result->fetch_array()) {
  $data[]=$row;
}
echo json_encode($data);

猜你喜欢

转载自www.cnblogs.com/ayanboke/p/9447730.html