Home Database Mysql Tutorial sql多表查询语句与方法

sql多表查询语句与方法

Jun 07, 2016 pm 05:46 PM

sql多表查询有很多种方法,如有自然连接 INNER JOIN,外边查询LEFT JOIN,交叉查询JOIN,交叉连接JOIN等join on left on 等多的是哦。

sql多表查询语句与方法

sql多表查询有很多种方法,如有自然连接 INNER JOIN,外边查询LEFT JOIN,交叉查询

JOIN,交叉连接JOIN等join on left on 等多的是哦。

下面使用等值连接列出authors和publishers表中位于同一城市的作者和出版社:

  
  Select *
  FROM authors AS a INNER JOIN publishers AS p
  ON a.city=p.city

 

又如使用自然连接,在选择列表中删除authors 和publishers 表中重复列(city和state)

  
  Select a.*,p.pub_id,p.pub_name,p.country
  FROM authors AS a INNER JOIN publishers AS p
  ON a.city=p.city

外边查询

Select a.*,b.* FROM luntan LEFT JOIN usertable as b
  ON a.username=b.username


  
下面使用全外连接将city表中的所有作者以及user表中的所有作者,以及他们所在的城市

  
  Select a.*,b.*
  FROM city as a FULL OUTER JOIN user as b
  ON a.username=b.username

交叉查询

交叉连接不带Where 子句,它返回被连接的两个表所有数据行的笛卡尔积,返回到结果集

合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的

数据行数。例,titles表中有6类图书,而publishers表中有8家出版社,则下列交叉连接

检索到的记录数将等于6*8=48行。

  
  Select type,pub_name
  FROM titles CROSS JOIN publishers
  ORDER BY type


使用左外连接将论坛内容和作者信息连接起来:

Select a.*,b.* FROM luntan LEFT JOIN usertable as b

ON a.username=b.username

 

下面使用全外连接将city表中的所有作者以及user表中的所有作者,以及他们所在的城市

Select a.*,b.*

FROM city as a FULL OUTER JOIN user as b

ON a.username=b.username


(三)交叉连接

交叉连接不带Where 子句,它返回被连接的两个表所有数据行的笛卡尔积,返回到结果集

合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的

数据行数。

例,titles表中有6类图书,而publishers表中有8家出版社,则下列交叉连接检索到的记

录数将等

于6*8=48行。

Select type,pub_name

FROM titles CROSS JOIN publishers

orDER BY type 

下面我们来看一个我写的多表查询吧

$sql = "Select zgy_jobs_faces.*,zgy_jobs_index.*,zgy_jobs_option.* from

zgy_jobs_faces,zgy_jobs_index,zgy_jobs_option where   zgy_jobs_option.mulplace

='$city' and zgy_jobs_faces.djobskinds ='$parttime' and zgy_jobs_faces.cid=

zgy_jobs_option.cid and zgy_jobs_option.cid = zgy_jobs_index.cid group by

zgy_jobs_faces.jname order by zgy_jobs_option.jid desc limit 0,30";

用group by 过滤重复的数据

关键词:sql查询,多表查询

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)

Multi-table related query skills in PHP Multi-table related query skills in PHP May 24, 2023 am 10:01 AM

Multi-table related query skills in PHP Related queries are an important part of database queries, especially when you need to display data in multiple related database tables. In PHP applications, multi-table related queries are often used when using databases such as MySQL. The meaning of multi-table association is to compare data in one table with data in another or multiple tables, and connect those rows that meet the requirements in the result. When performing multi-table correlation queries, you need to consider the relationship between tables and use appropriate correlation methods. The following introduces several types of

MyBatis multi-table query optimization: methods and strategies to improve SQL performance MyBatis multi-table query optimization: methods and strategies to improve SQL performance Feb 18, 2024 pm 10:24 PM

In-depth analysis of MyBatis multi-table queries: tips and strategies for optimizing SQL performance Summary: MyBatis is a commonly used persistence layer framework that can help us operate the database more conveniently. In actual development, multi-table query is a very common requirement, but performing multi-table query in an inappropriate way may lead to performance degradation. This article will focus on how to use MyBatis for multi-table queries, and give some tips and strategies for optimizing SQL performance. Introduction MyBatis is a popular Java persistence layer framework,

Master MyBatis multi-table queries: a practical guide to optimizing data processing efficiency Master MyBatis multi-table queries: a practical guide to optimizing data processing efficiency Feb 19, 2024 pm 06:36 PM

Comprehensively master MyBatis multi-table query: a practical guide to improve data processing efficiency Introduction: Today, in software development, data processing efficiency is an important consideration. For data processing involving multi-table queries, MyBatis is a powerful tool. This article will delve into how to fully master MyBatis multi-table queries and improve the efficiency of data processing. The article will demonstrate through specific code examples to help readers better understand and apply. 1. Configure the MyBatis environment. First, we need to configure My

Meituan interview question: Have you ever encountered slow SQL? How was it solved? Meituan interview question: Have you ever encountered slow SQL? How was it solved? Aug 24, 2023 pm 03:41 PM

MySQL's slow query log is a log record provided by MySQL. It is used to record statements in MySQL whose query time exceeds (greater than) the set threshold (long_query_time) and records them in the slow query log.

PHP and PDO: How to execute complex SQL queries PHP and PDO: How to execute complex SQL queries Jul 28, 2023 pm 03:43 PM

PHP and PDO: How to execute complex SQL query statements When processing database operations, PHP provides a powerful extension library PDO (PHPDataObjects) to simplify interaction with the database. PDO supports a variety of databases, such as MySQL, SQLite, etc., and also provides a wealth of functions and methods to facilitate developers to perform various database operations. This article will introduce how to use PDO to execute complex SQL query statements, and attach corresponding code examples. Connect to the database

Analyze common problems in MyBatis multi-table association queries: Solve doubts in data connection queries Analyze common problems in MyBatis multi-table association queries: Solve doubts in data connection queries Feb 18, 2024 am 10:41 AM

Analysis of common problems in MyBatis multi-table query: To solve the confusion in data correlation query, specific code examples are needed. Introduction: In database application development, correlation query between data tables is a very common requirement. For the MyBatis framework, multi-table query is a very important function. However, due to the flexibility and powerful dynamic SQL capabilities of MyBatis, sometimes developers may encounter some confusion when performing multi-table queries. This article will describe some common problems and provide specific code examples to solve them.

What are the common table operations in PHP programming? What are the common table operations in PHP programming? Jun 12, 2023 am 09:46 AM

In web development, tables are the most basic and commonly used elements, and PHP is a popular server-side programming language. There are many common techniques and methods in table operations. This article will introduce common table operations in PHP programming. Displaying data tables In PHP, you can use the table tag in HTML to display data tables. It is worth noting that the table must be generated in a PHP script. Here is an example of a basic HTML table tag: <table><tr>

Why does my Go program error when executing SQL queries? Why does my Go program error when executing SQL queries? Jun 09, 2023 pm 06:10 PM

In Go programming, using SQL queries is a common task. However, sometimes errors occur when executing SQL queries, causing the program to fail to execute correctly. In order to solve these errors, we need to have a deep understanding of how SQL queries and the Go language interact. Below are some possible errors and corresponding solutions. Lack of database driver In Go language, you need to use a specific database driver to connect and operate the database. If you try to perform a database query and the database driver is not properly installed and configured

See all articles