php mysqli 查詢個門店數據(面向對象)

---恢復內容開始---php

最近有一張以下的表,因爲圖片比較大,我改寫爲sql,以下所示:html

SET FOREIGN_KEY_CHECKS=0;mysql

-- ----------------------------
-- 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='機構基礎信息表';sql

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');json

須要查詢出來的結果要求是,把表內容按照省市區門店的方式列出來,而且要求:北京——豐臺——某某門店,一對多的關係,不能出現兩個北京豐臺。fetch

查詢代碼以下:code


<?phphtm

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);utf-8

相關文章
相關標籤/搜索