1. 程式人生 > >YAF根據條件拼接SQL語句

YAF根據條件拼接SQL語句

                                                                  YAF根據條件拼接SQL語句
                    
1、建user表
    CREATE TABLE `user` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵ID自增',
      `name` varchar(50) NOT NULL DEFAULT '' COMMENT '使用者名稱',
      `age` int(11) NOT NULL DEFAULT '0' COMMENT '年齡',
      `type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '型別  1:普通使用者 2:VIP使用者 3:管理員 預設1',
      `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '狀態   1:正常 2:違規刪除 預設1',
      PRIMARY KEY (`id`),
      KEY `id` (`id`) USING BTREE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='使用者表';
    
2、往表中插入資料後(截圖)
    INSERT INTO `user` VALUES ('1', '王1', '17', '1', '1');
    INSERT INTO `user` VALUES ('2', '王2', '18', '1', '1');
    INSERT INTO `user` VALUES ('3', '王3', '20', '2', '1');
    INSERT INTO `user` VALUES ('4', '王4', '19', '1', '1');
    INSERT INTO `user` VALUES ('5', '王5', '16', '2', '1');
    INSERT INTO `user` VALUES ('6', '王6', '21', '1', '1');
    INSERT INTO `user` VALUES ('7', '王7', '21', '2', '1');
    INSERT INTO `user` VALUES ('8', '王8', '19', '2', '1');
    INSERT INTO `user` VALUES ('9', '王9', '19', '2', '1');
    INSERT INTO `user` VALUES ('10', '王10', '23', '1', '2');
    
    效果截圖
    


3、PHP程式碼

/**
	 * @todo 找出
	 * 狀態為正常,
	 * 姓名中包含王字,
	 * 年齡在17-19歲 
	 * 普通使用者 或 VIP使用者
	 * 按照主鍵ID降序
	 * 的 記錄數與總數
	 */
	function selectAction(){
		$where = $orwhere = [];
		$where[] = ['status', 1];
		//$where[] = ['type', '=', 1];	//與上一句等價
		$where[] = ['name', 'like', '%王%'];
		$where[] = ['age', 'in', [17,18,19]];
		
		$orwhere = $where;
		$where[] = ['type', 1];
		$orwhere[] = ['type', 2];
		
		$whereSql = $this->wheresql($where);
		$orWhereSql = $this->wheresql($orwhere);
		
		$field = "id,name,age,type,status";
		$sql =  " select {$field}".
				" from user".
				" where 1=1 {$whereSql} or 1=1 {$orWhereSql}".
				" order by id desc ";	//."limit 0,2";
		$mUser = new UserModel();
		$list = $mUser->query($sql);
		
		$sqlCount = " select count(*) as count ".
					" from user".
					" where 1=1 {$whereSql} or 1=1 {$orWhereSql}";
		$count = $mUser->query($sqlCount);
		$count = $count[0]['count'];
		
		//列印的結果
		echo "{$sql}<br/>";
		//select id,name,age,type,status from user where 1=1 AND status = "1" AND name like "%王%" AND age in (17,18,19) AND type = "1" or 1=1 AND status = "1" AND name like "%王%" AND age in (17,18,19) AND type = "2" order by id desc 
		echo "{$sqlCount}<br/>";
		// select count(*) as count from user where 1=1 AND status = "1" AND name like "%王%" AND age in (17,18,19) AND type = "1" or 1=1 AND status = "1" AND name like "%王%" AND age in (17,18,19) AND type = "2"
		var_export($list);
		echo "<br/>";
		/* array ( 0 => array ( 'id' => 9, 'name' => '王9', 'age' => 19, 'type' => 2, 'status' => 1, ), 
				1 => array ( 'id' => 8, 'name' => '王8', 'age' => 19, 'type' => 2, 'status' => 1, ), 
				2 => array ( 'id' => 4, 'name' => '王4', 'age' => 19, 'type' => 1, 'status' => 1, ), 
				3 => array ( 'id' => 2, 'name' => '王2', 'age' => 18, 'type' => 1, 'status' => 1, ), 
				4 => array ( 'id' => 1, 'name' => '王1', 'age' => 17, 'type' => 1, 'status' => 1, ), ) */
		var_export($count);	//5
		die;
	}
	
	/**
	 * @todo YAF where條件陣列轉字串
	 * @param unknown $where
	 * @return string
	 */
	function wheresql($where){
		$whereSql = '';
		if( $where ){	//拼接where條件
			foreach ( $where as $k => $v){
				if(count($v) == 2){	//預設是等於號
					$whereSql .= ' AND '.$v[0].' = "'.$v[1].'"';
				}else{
					if(strtoupper($v[1]) == "IN"){	//加入為in的判斷
						$whereSql .= ' AND '.$v[0].' in (';
						for ( $x=0; $x<count($v[2]); $x++ ){
							if($x==(count($v[2])-1)){	//最後一個不要點
								$whereSql .= $v[2][$x];
							}else{
								$whereSql .= $v[2][$x].',';
							}
						}
						$whereSql .=') ';
					}else{
						$whereSql .= ' AND '.$v[0].' '.$v[1].' "'.$v[2].'"';
					}
				}
			}
		}
		return $whereSql;
	}