Home Web Front-end JS Tutorial CI框架数据库查询之join用法分析_php实例

CI框架数据库查询之join用法分析_php实例

Jun 07, 2016 pm 05:07 PM
ci framework join Database query

本文实例讲述了CI框架数据库查询之join用法。分享给大家供大家参考,具体如下:

用 A表中的每个ID 去查询这个 ID 在 people 表中的信息。语句如下:

$this->db->from('A');
$this->db->join('B', 'sites.id = B.id');

Copy after login

用 A表中的每个ID 去查询这个 ID 在 B表中的信息。

注意SQL的约定,如果一个列名在二张表中是重复的,你需要在列名前加上表名和一个“."号。因此sites.id在位置桌子中意谓id所在的表是sites。在进行SQL多表查询时,最好把列名进行唯一性的标识,这样可以避免产生岐义,也可以让你自己明了。

如:你执行以下语句

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();

Copy after login

相当于 执行这条sql语句

SELECT * FROM blogs JOIN comments ON comments.id = blogs.id

Copy after login

如果你想要在查询中使用多个连接,可以多次调用本函数。

如果你需要指定 JOIN 的类型,你可以通过本函数的第三个参数来指定。可选项包括:left, right, outer, inner, left outer, 以及 right outer.

$this->db->join('comments', 'comments.id = blogs.id', 'left');
// 生成: LEFT JOIN comments ON comments.id = blogs.id

Copy after login

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

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
3 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)

How to use JOIN in MySql How to use JOIN in MySql Jun 04, 2023 am 08:02 AM

The meaning of JOIN is just like the English word "join". It joins two tables and can be roughly divided into inner join, outer join, right join, left join and natural join. First create two tables, the following is used as an example CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+------+--------+| id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

What is the usage principle of MySQL Join? What is the usage principle of MySQL Join? May 26, 2023 am 10:07 AM

Join type leftjoin uses the left table as the driving table and the left table as the basis of the result set. The data from the right table is connected to the result set. rightjoin uses the right table as the driving table and the right table as the basis of the result set to connect the left table. The data is added to the result set innerjoin. The result set takes the intersection of the two tables fulljoin. The result set takes the union of the two tables. MySQL does not have a fulljoin. The difference between union and unionall is that union will deduplicate the crossjoin Cartesian product. If the where condition is not used, the result set will be the product and of the two associated table rows. The difference is that when crossjoin creates the result set, it will be passed according to the on condition.

How to use CI framework in PHP How to use CI framework in PHP Jun 27, 2023 pm 04:51 PM

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

How to solve the problem of database query number overflow in Java development How to solve the problem of database query number overflow in Java development Jun 29, 2023 pm 06:46 PM

How to solve the problem of database query number overflow in Java development. Title: How to solve the database query number overflow problem in Java development. Abstract: With the development of the Internet and the gradual increase in the amount of data, the number of database queries is also increasing. In Java development, due to memory limitations, you may encounter the problem of overflow in the number of database queries. This article will introduce several ways to solve this problem. Text: Optimizing database query statements First, we can solve this problem from the perspective of optimizing database query statements. we can use

How to query a database and display the results using PHP How to query a database and display the results using PHP May 02, 2024 pm 02:15 PM

Steps to use PHP to query the database and display the results: connect to the database; query the database; display the results, traverse the rows of the query results and output specific column data.

How to use CI4 framework in php? How to use CI4 framework in php? Jun 01, 2023 pm 02:40 PM

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

Laravel middleware: Add database querying and performance monitoring to your application Laravel middleware: Add database querying and performance monitoring to your application Jul 28, 2023 pm 02:53 PM

Laravel Middleware: Adding Database Query and Performance Monitoring to Applications Introduction: Data query and performance monitoring are very important when developing web applications. Laravel provides a convenient way to handle these requirements, namely middleware. Middleware is a technology that handles between requests and responses. It can perform some logic before the request reaches the controller or after the response is returned to the user. This article will introduce how to use Laravel middleware to implement database query and performance monitoring. 1. Create the middle

See all articles