Home Database Mysql Tutorial Mysql使用自定义方法以及cakephp分页使用join查询的方法_MySQL

Mysql使用自定义方法以及cakephp分页使用join查询的方法_MySQL

Jun 01, 2016 pm 01:37 PM
cakephp

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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

How to use the database query builder in CakePHP? How to use the database query builder in CakePHP? Jun 04, 2023 am 09:02 AM

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

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

How does CakePHP handle file uploads? How does CakePHP handle file uploads? Jun 04, 2023 pm 07:21 PM

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

How to use Twig with CakePHP? How to use Twig with CakePHP? Jun 05, 2023 pm 07:51 PM

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

See all articles