Mysql inner join on的用法實(shí)例(必看)
語法規(guī)則
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name
先創(chuàng)建兩個(gè)表,1.用戶,2.用戶類別
用戶表
CREATE TABLE `user` ( `id` int(32) NOT NULL AUTO_INCREMENT, `name` varchar(16) NOT NULL, `kindid` int(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
用戶類別表
CREATE TABLE `userkind` ( `id` int(32) NOT NULL AUTO_INCREMENT, `kindname` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
插入一些數(shù)據(jù)到user表
INSERT INTO `user` VALUES (1,'小明',1),(2,'小紅',1),(3,'涵涵',2);插入一些數(shù)據(jù)到 userkind表
INSERT INTO `userkind` VALUES (1,'普通會(huì)員'),(2,'VIP會(huì)員');
如圖:
下面是控制臺(tái)的查詢例子:
Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.40 MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use join; Database changed mysql> select * from `user`; +----+------+--------+ | id | name | kindid | +----+------+--------+ | 1 | 小明 | 1 | | 2 | 小紅 | 1 | | 3 | 涵涵 | 2 | +----+------+--------+ 3 rows in set (0.00 sec) mysql> select * from `userkind`; +----+----------+ | id | kindname | +----+----------+ | 1 | 普通會(huì)員 | | 2 | VIP會(huì)員 | +----+----------+ 2 rows in set (0.00 sec) mysql> select * from `user` inner join `userkind` on user.kindid=userkind.id; +----+------+--------+----+----------+ | id | name | kindid | id | kindname | +----+------+--------+----+----------+ | 1 | 小明 | 1 | 1 | 普通會(huì)員 | | 2 | 小紅 | 1 | 1 | 普通會(huì)員 | | 3 | 涵涵 | 2 | 2 | VIP會(huì)員 | +----+------+--------+----+----------+ 3 rows in set (0.02 sec) mysql> select `id` as `用戶ID`,`name` as `用戶名`,`kindname` as `用戶類別` from `user` inner join `userkind` where user.kindid=userkind.id; ERROR 1052 (23000): Column 'id' in field list is ambiguous mysql> select `user`.`id` as `用戶ID`,`name` as `用戶名`,`kindname` as `用戶類別 ` from -> `user` inner join `userkind` where `user`.`kindid`=`userkind`.`id`; +--------+--------+----------+ | 用戶ID | 用戶名 | 用戶類別 | +--------+--------+----------+ | 1 | 小明 | 普通會(huì)員 | | 2 | 小紅 | 普通會(huì)員 | | 3 | 涵涵 | VIP會(huì)員 | +--------+--------+----------+ 3 rows in set (0.00 sec) mysql> select `user`.`id` as `用戶ID`,`name` as `用戶名`,`kindname` as `用戶類別 ` from `user` inner join `userkind` on `user`.`kindid`=`userkind`.`id`; +--------+--------+----------+ | 用戶ID | 用戶名 | 用戶類別 | +--------+--------+----------+ | 1 | 小明 | 普通會(huì)員 | | 2 | 小紅 | 普通會(huì)員 | | 3 | 涵涵 | VIP會(huì)員 | +--------+--------+----------+ 3 rows in set (0.00 sec) mysql>
需要注意的是: 這里的on 基本等價(jià)于where(本人感覺)
當(dāng) column (字段) 兩個(gè)表都有 卻分不清時(shí),需要用`表名`.`字段名` 進(jìn)行分辨。
as就是取別名了。看上面例子就知道!
以上這篇Mysql inner join on的用法實(shí)例(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SQL中INNER JOIN的實(shí)現(xiàn)
- MySQL INNER JOIN 的底層實(shí)現(xiàn)原理分析
- MySQL中的 inner join 和 left join的區(qū)別解析(小結(jié)果集驅(qū)動(dòng)大結(jié)果集)
- SQL聯(lián)合查詢inner join、outer join和cross join的區(qū)別詳解
- SQL之left join、right join、inner join的區(qū)別淺析
- MySQL中視圖的使用及多表INNER JOIN的技巧分享
- MYSQL使用inner join 進(jìn)行 查詢/刪除/修改示例
- 超詳細(xì)mysql left join,right join,inner join用法分析
- MSSQL內(nèi)外連接(INNER JOIN)語句詳解
- SQL中的INNER JOIN操作方法
相關(guān)文章
MySQL使用Partition功能實(shí)現(xiàn)水平分區(qū)的策略
這篇文章主要介紹了MySQL使用Partition功能實(shí)現(xiàn)水平分區(qū),給大家提到了水平分區(qū)的5種策略,通過sql語句給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12mysql數(shù)據(jù)庫常見基本操作實(shí)例分析【創(chuàng)建、查看、修改及刪除數(shù)據(jù)庫】
這篇文章主要介紹了mysql數(shù)據(jù)庫常見基本操作,結(jié)合實(shí)例形式分析了mysql創(chuàng)建、查看、修改及刪除數(shù)據(jù)庫實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04mysql批量插入BulkCopy的實(shí)現(xiàn)
本文主要介紹了mysql批量插入BulkCopy的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03