连表查询 1对多

如果是一对多的

在表结构上要关联:在多的那个表中添加一个字段,关联一的那个表中的ID


drop table if exists p40_goods;
create table p40_goods
(
id mediumint unsigned not null auto_increment comment 'Id',
goods_name varchar(150) not null comment '商品名称',
market_price decimal(10,2) not null comment '市场价格',
shop_price decimal(10,2) not null comment '本店价格',
goods_desc longtext comment '商品描述',
is_on_sale enum('是','否') not null default '是' comment '是否上架',
is_delete enum('是','否') not null default '否' comment '是否放到回收站',
addtime datetime not null comment '添加时间',
logo varchar(150) not null default '' comment '原图',
sm_logo varchar(150) not null default '' comment '小图',
mid_logo varchar(150) not null default '' comment '中图',
big_logo varchar(150) not null default '' comment '大图',
mbig_logo varchar(150) not null default '' comment '更大图',
baind_id mediumint unsigned not null default '0' comment '品牌id',
primary key (id),
key shop_price(shop_price),
key addtime(addtime),
key brand_id(brand_id),
key is_on_sale(is_on_sale)
)engine=InnoDB default charset=utf8 comment '商品';

alter table p40_goods add brand_id mediumint unsigned not null default '0' comment '品牌id';

alter table p40_goods add key  brand_id(brand_id);

程序代码上要关联:

  1. 添加商品时选择品牌

 1.1. 在控制器中取出所有的品牌

 

 

//取出所有的品牌
$brandModel=D('brand');
$brandData=$brandModel->select();

// 设置页面信息
$this->assign(array(
'brandData'=> $brandData,
'_page_title' => '添加新商品',
'_page_btn_name' => '商品列表',
'_page_btn_link' => U('lst'),
));

在添加表单中循环制作下拉框

   

<tr>
<td class="label">所在品牌:</td>

<td>
<select name="brand_id" id="">
<option value="">请选择</option>
<?php foreach($brandData as $k =>$v): ?>
<option value=" <?php echo $v['id'] ; ?> "> <?php echo $v['brand_name']; ?> </option>
<?php endforeach; ?>
</select>

</td>
</tr>

提交表单时把品牌ID保存到 商品表中的brand_id字段,修改商品模型设置允许接收表单中的品牌ID字段

 

// 添加时调用create方法允许接收的字段
protected $insertFields = 'goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id';
// 修改时调用create方法允许接收的字段
protected $updateFields = 'id,goods_name,market_price,shop_price,is_on_sale,goods_desc,brand_id';

  1. 在商品列表中把商品所在的品牌名称显示出来

2.1 lst.html中添加一列

   

<tr>
<th>编号</th>
<th>品牌</th>
<th>商品名称</th>
<th>logo</th>
<th>市场价格</th>
<th>本店价格</th>
<th>上架</th>
<th>添加时间</th>
<th>操作</th>
</tr>

 <td align="center"><?php echo $v['brand_id']; ?></td>

商品表中保存的是品牌的ID,显示iD没有意义,所以需要在取商品时连表根据品牌ID查询出品牌的名称:

  1. 取商品时连表取出品牌名称【* 连表操作是做项目的基础,一定要熟练掌握!!!!!】

修改商品模型中的search方法中取数据的代码:

 

 

/************** 取某一页的数据 ***************/
/***********
*
* selecta.*,b.brand_name from p40_goods a left join p40_brand on a.brand_id=b.id
*
******/
$data = $this->order("$orderby $orderway") // 排序
->fields('a.*,b.brand_name')
->alias('a')
->join('LEFT JOIN __BRAND__ b on a.brand_id=b.id')
->where($where) // 搜索
->limit($pageObj->firstRow.','.$pageObj->listRows) // 翻页
->select();

因为这里连了表并取了别名,所以前面的搜索和排序时要指定根据哪个表中的哪个字段:

排序时:

 

/***************** 排序 *****************/
$orderby = 'a.id'; // 默认的排序字段
$orderway = 'desc'; // 默认的排序方式
$odby = I('get.odby');
if($odby)
{
if($odby == 'id_asc')
$orderway = 'asc';
elseif ($odby == 'price_desc')
$orderby = 'shop_price';
elseif ($odby == 'price_asc')
{
$orderby = 'shop_price';
$orderway = 'asc';
}
}

搜索时:

 

/*************** 搜索 ******************/
$where = array(); // 空的where条件
// 商品名称
$gn = I('get.gn');
if($gn)
$where['a.goods_name'] = array('like', "%$gn%"); // WHERE goods_name LIKE '%$gn%'
// 价格
$fp = I('get.fp');
$tp = I('get.tp');
if($fp && $tp)
$where['a.shop_price'] = array('between', array($fp, $tp)); // WHERE shop_price BETWEEN $fp AND $tp
elseif ($fp)
$where['a.shop_price'] = array('egt', $fp); // WHERE shop_price >= $fp
elseif ($tp)
$where['a.shop_price'] = array('elt', $tp); // WHERE shop_price <= $fp
// 是否上架
$ios = I('get.ios');
if($ios)
$where['a.is_on_sale'] = array('eq', $ios); // WHERE is_on_sale = $ios
// 添加时间
$fa = I('get.fa');
$ta = I('get.ta');
if($fa && $ta)
$where['addtime'] = array('between', array($fa, $ta)); // WHERE shop_price BETWEEN $fp AND $tp
elseif ($fa)
$where['addtime'] = array('egt', $fa); // WHERE shop_price >= $fp
elseif ($ta)
$where['addtime'] = array('elt', $ta); // WHERE shop_price <= $fp

修改页面

   <td align="center"><?php echo $v['brand_name']; ?></td>

 

 

 

 

世上无难事,只怕有心人......
原文地址:https://www.cnblogs.com/gooderic/p/5682762.html