Mysql使用自定义方法以及cakephp分页使用join查询的方法_MySQL
bitsCN.com
Mysql使用自定义方法以及cakephp分页使用join查询的方法
第一步:设置SET GLOBAL log_bin_trust_function_creators=TRUE;
如果报ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)这种错误
第二步:
Sql代码
DELIMITER $$
USE `zhiku`$$
DROP FUNCTION IF EXISTS `getChildDept`$$
CREATE FUNCTION `getChildDept`(rootId INT) RETURNS TEXT CHARSET utf8
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = '$';
SET sTempChd =CAST(rootId AS CHAR);
WHILE sTempChd IS NOT NULL DO
SET sTemp = CONCAT(sTemp,',',sTempChd);
SELECT GROUP_CONCAT(id) INTO sTempChd FROM zk_departments WHERE FIND_IN_SET(parent_id,sTempChd)>0;
END WHILE;
RETURN sTemp;
END$$
DELIMITER ;
第三步:直接调用
SELECT DISTINCT(d.user_id) AS user_id,d.dept_id,u.compellation FROM zk_user_departments d INNER JOIN zk_users u ON u.id=d.user_id AND INSTR(u.pinyin,'h')=2 WHERE FIND_IN_SET(d.dept_id, getChildDept(128)) GROUP BY d.user_id;
放在cakephp为:
Php代码
$conditions = array('FIND_IN_SET(dept_id, getChildDept('.$dept_id.'))');
$condition_join = '`User`.`id` = `UserDepartment`.`user_id`';
if(!emptyempty($c))$condition_join .= ' AND INSTR(User.pinyin,"'.$c.'")=2';
//分页
$this->paginate = array(
'UserDepartment' => array(
'conditions' => $conditions,
'order' => array('dept_id'=>'ASC'),
'limit' => 10,
'recursive' => -1,
'group' => array('user_id'),
'fields' => array('user_id','dept_id'),
'joins' => array(array(
'alias' => 'User',
'table' => 'zk_users',
'type' => 'INNER',
'conditions' => $condition_join,
)),
)
);
$data = $this->paginate('UserDepartment');
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open source PHPMVC framework which is widely used in web application development. CakePHP has many features and tools, including a powerful database query builder for interactive performance databases. This query builder allows you to execute SQL queries using object-oriented syntax without having to write cumbersome SQL statements. This article will introduce how to use the database query builder in CakePHP. Establishing a database connection Before using the database query builder, you first need to create a database connection in Ca

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open source web application framework built on the PHP language that simplifies the development process of web applications. In CakePHP, processing file uploads is a common requirement. Whether it is uploading avatars, pictures or documents, the corresponding functions need to be implemented in the program. This article will introduce how to handle file uploads in CakePHP and some precautions. Processing uploaded files in Controller In CakePHP, uploaded files are usually processed in Cont

Using Twig in CakePHP is a way to separate templates and views, making the code more modular and maintainable. This article will introduce how to use Twig in CakePHP. 1. Install Twig. First install the Twig library in the project. You can use Composer to complete this task. Run the following command in the console: composerrequire "twig/twig:^2.0" This command will be displayed in the project's vendor
