饿虎岗资源网 Design By www.oxmxm.com
语法规则
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name
先创建两个表,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;
插入一些数据到user表
INSERT INTO `user` VALUES (1,'小明',1),(2,'小红',1),(3,'涵涵',2);插入一些数据到 userkind表
INSERT INTO `userkind` VALUES (1,'普通会员'),(2,'VIP会员');
如图:
下面是控制台的查询例子:
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 | 普通会员 | | 2 | VIP会员 | +----+----------+ 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 | 普通会员 | | 2 | 小红 | 1 | 1 | 普通会员 | | 3 | 涵涵 | 2 | 2 | VIP会员 | +----+------+--------+----+----------+ 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 | 小明 | 普通会员 | | 2 | 小红 | 普通会员 | | 3 | 涵涵 | VIP会员 | +--------+--------+----------+ 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 | 小明 | 普通会员 | | 2 | 小红 | 普通会员 | | 3 | 涵涵 | VIP会员 | +--------+--------+----------+ 3 rows in set (0.00 sec) mysql>
需要注意的是: 这里的on 基本等价于where(本人感觉)
当 column (字段) 两个表都有 却分不清时,需要用`表名`.`字段名` 进行分辨。
as就是取别名了。看上面例子就知道!
以上这篇Mysql inner join on的用法实例(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 曾轶可.2011-一只猫的旅行【天娱传媒】【WAV+CUE】
- 黑神话悟空上品狼刺客精魄获取方法一览|上品狼刺客精魄收集攻略
- 视觉小说《神椿市建设中REGENERATE》将于2025年2月20日发售
- 黑神话悟空上品鼠弩手精魄获取方法一览|上品鼠弩手精魄收集攻略
- 中国音乐地图听见世界非物质文化遗产《非遗HQCD》wav
- 中国民乐合奏作品集《华夏之音HQ》头版限量编号[WAV+CUE]
- [电影原声带]黎允文《天将雄师》[FLAC+CUE]
- 曝NS继任机型已进入量产阶段:已花了30亿日元
- 不是Switch2?曝《马里奥惊奇》NS捆绑包即将公开
- 暖心任天堂!网友Switch维修返还时贴纸被完好保留
- 《国语老歌 经典对唱情歌 2CD》[WAV/分轨][1.1GB]
- 《刀郎 翻唱精选华语专辑 披着羊皮的狼 》[WAV+CUE][430MB]
- 《真的田震 中国乐坛红极一时的歌者 精品集》[WAV+CUE][300MB]
- 群星.1995-新滚石九大天王之情歌大全【滚石】【WAV+CUE】
- 群星.1993-滚石九大天王十二出好戏·纵夏欢唱【滚石】【WAV+CUE】