検索

本文实例讲述了mysql的左右内连接用法。分享给大家供大家参考。具体如下:

用个例子来解析下mysql的左连接, 右连接和内连接

代码如下:

create table user_id ( id decimal(18) );
create table user_profile ( id decimal(18) , name varchar(255) ) ;
insert into user_id values (1);
insert into user_id values (2);
insert into user_id values (3);
insert into user_id values (4);
insert into user_id values (5);
insert into user_id values (6);
insert into user_id values (1);

insert into user_profile values (1, "aa");
insert into user_profile values (2, "bb");
insert into user_profile values (3, "cc");
insert into user_profile values (4, "dd");
insert into user_profile values (5, "ee");
insert into user_profile values (5, "EE");
insert into user_profile values (8, 'zz');

一. 左连接:

代码如下:

mysql> select a.id id , ifnull(b.name, 'N/A') name from user_id a left join user_profile b on a.id = b.id;
mysql> select a.id id , ifnull(b.name, 'N/A') name from user_id a left join user_profile b on a.id = b.id;   
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
|    4 | dd   |
|    5 | ee   |
|    5 | EE   |
|    6 | N/A  |
|    1 | aa   |
+------+------+
8 rows in set (0.00 sec)

user_id居左,故谓之左连接。 这种情况下,以user_id为主,即user_id中的所有记录均会被列出。分以下三种情况:

1. 对于user_id中的每一条记录对应的id如果在user_profile中也恰好存在而且刚好只有一条,那么就会在返回的结果中形成一条新的记录。如上面1, 2, 3, 4对应的情况。
2. 对于user_id中的每一条记录对应的id如果在user_profile中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的5对应的情况。
3. 对于user_id中的每一条记录对应的id如果在user_profile中不存在,那么就会在返回的结果中形成一条条新的记录,且该记录的右边全部NULL。如上面的6对应的情况。

不符合上面三条规则的记录不会被列出。

比如, 要查询在一个相关的表中不存在的数据, 通过id关联,要查出user_id表中存在user_profile中不存在的记录:

代码如下:

select count(*) from user_id left join user_profile on user_id.id = user_profile.id where user_profile.id is null;

二. 右连接

user_profile居右,故谓之右连接。 这种情况下, 以user_profile为主,即user_profile的所有记录均会被列出。分以下三种情况:

1. 对于user_profile中的每一条记录对应的id如果在user_id中也恰好存在而且刚好只有一条,那么就会在返回的结果中形成一条新的记录。如上面2, 3, 4, 5对应的情况。
2. 对于user_profile中的每一条记录对应的id如果在user_id中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的1对应的情况。
3. 对于user_profile中的每一条记录对应的id如果user_id中不存在,那么就会在返回的结果中形成一条条新的记录,且该记录的左边全部NULL。如上面的8对应的情况。

不符合上面三条规则的记录不会被列出。

三. 内连接

MySQL内连接的数据记录中,不会存在字段为NULL的情况。可以简单地认为,内链接的结果就是在左连接或者右连接的结果中剔除存在字段为NULL的记录后所得到的结果, 另外,MySQL不支持full join

代码如下:

mysql> select *  from user_id a inner join user_profile b on a.id = b.id;
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)
mysql> select * from user_id a, user_profile b where a.id = b.id;                   
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)
mysql> select *  from user_id a join user_profile b on a.id = b.id;    
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)

希望本文所述对大家的MySQL程序设计有所帮助。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
mysql blob:制限はありますか?mysql blob:制限はありますか?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes)、blob(65,535bytes)、mediumblob(16,777,215bytes)、andlongblob(4,294,967,295bytes).tousebl難易度:1)PROFFORMANCESANDSTORERGEBLOBSEXTERNALLY;

MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?MySQL:ユーザーの作成を自動化するための最良のツールは何ですか?May 08, 2025 am 12:22 AM

MySQLでユーザーの作成を自動化するための最良のツールとテクノロジーには、次のものがあります。1。MySQLWorkBench、中小サイズの環境に適した、使いやすいがリソース消費量が高い。 2。アンシブル、マルチサーバー環境に適した、シンプルだが急な学習曲線。 3.カスタムPythonスクリプト、柔軟性がありますが、スクリプトセキュリティを確保する必要があります。 4。大規模な環境に適した人形とシェフ、複雑ですがスケーラブル。選択する際には、スケール、学習曲線、統合のニーズを考慮する必要があります。

mysql:blob内で検索できますか?mysql:blob内で検索できますか?May 08, 2025 am 12:20 AM

はい、youcansearchinsideablobinmysqlusingspecifictechniques.1)converttheblobtoautf-8stringwithconvert function andsearchusinglike.2)

MySQL文字列データ型:包括的なガイドMySQL文字列データ型:包括的なガイドMay 08, 2025 am 12:14 AM

mysqloffersvariousstringdatypes:1)charfofixed-lengthstrings、italforconsentlengtalikecountrycodes; 2)varcharforvariable-lengthstrings、適切なForfieldslikenames;

MySQLブロブのマスター:ステップバイステップのチュートリアルMySQLブロブのマスター:ステップバイステップのチュートリアルMay 08, 2025 am 12:01 AM

tomastermysqlblobs、soflowthesesteps:1)shoseetheapsosupturateblobtype(tinyblob、blob、mediumblob、longblob)basedOndatasize.2)insertDatausingload_fileforefficiency.3)storefilereferenceinsinsteadoffilestoimpeperformance.4)

MySQLのBLOBデータ型:開発者の詳細な概要MySQLのBLOBデータ型:開発者の詳細な概要May 07, 2025 pm 05:41 PM

blobdatatypesinmysqlareusedlarginglaredatalikeimagesorudio.1)useblobtypes(tinyblobtolongblob)Basedatasizeneeds。 2)storeblobsin perplate petooptimize performance.3)scondididididididididersxternalストレージBlob Romanaデータベースindimprovebackupe

コマンドラインからMySQLにユーザーを追加する方法コマンドラインからMySQLにユーザーを追加する方法May 07, 2025 pm 05:01 PM

toadduserstomysqlfromthecommandline、loginasroot、thenusecreateuser'username '@' host'ident'ident'identifidedby'password '; tocreateanewuser.grantpermissions with grantpermissions with grantalgegesondatabase

mysqlの文字列データ型は何ですか?詳細な概要mysqlの文字列データ型は何ですか?詳細な概要May 07, 2025 pm 03:33 PM

mysqlofferseightStringDatatypes:char、varchar、binary、varbinary、blob、text、enum、andset.1)charisfixed-length、yealforconsistent datalikecountrycodes.2)varcharisvariable length、efficational forvaryingdatalikenames.3)binaryandvanterbinarydata a similati

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン