请教 一个表里有3个不同的用户id 如何通过联用户表的方式 取出这三个用户名呢

yi890410 2014-03-31 10:18:49
RT:

大致场景是这样,我有一个app表

CREATE TABLE IF NOT EXISTS `ch_app` (
`id` int(10) unsigned NOT NULL auto_increment,
`appname` varchar(100) NOT NULL COMMENT 'app名称',
`appenname` varchar(50) NOT NULL COMMENT 'APP名称英文首字母',
`tid` mediumint(5) NOT NULL COMMENT 'app分类',
`uid` int(11) NOT NULL COMMENT '商务经理',
`manager` int(11) NOT NULL COMMENT '渠道经理',
`channel_id` int(11) NOT NULL default '0' COMMENT '渠道商',
`reference` varchar(30) NOT NULL COMMENT 'app来源',
`package_name` varchar(255) NOT NULL COMMENT 'app包名',
`juwan_id` int(11) NOT NULL default '0' COMMENT '聚玩appid',
`downloadurl` varchar(255) NOT NULL COMMENT 'app下载路径',
`commission` enum('1','2') NOT NULL COMMENT '佣金类型:1,CPA;2:CPS',
`commissionval` decimal(8,2) unsigned NOT NULL default '0.00' COMMENT '佣金值',
`size` decimal(10,3) NOT NULL COMMENT '大小',
`system` enum('1','2') NOT NULL default '1' COMMENT '系统要求:1,android;2,ios',
`sort` int(11) NOT NULL default '0' COMMENT '排序',
`details` varchar(500) NOT NULL COMMENT '详细信息',
`edituptime` int(11) NOT NULL COMMENT '更新时间',
`appuptime` int(11) NOT NULL COMMENT 'app安装包更新时间',
`flag` tinyint(1) NOT NULL COMMENT 'app状态:0,未上架;1,上架;2,下架',
PRIMARY KEY (`id`),
KEY `app_enname` (`appenname`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='app管理表' AUTO_INCREMENT=13 ;


和用户表:

CREATE TABLE IF NOT EXISTS `ch_user` (
`uid` int(11) NOT NULL auto_increment,
`username` varchar(50) default NULL,
`password` varchar(50) default NULL,
`group` tinyint(2) default NULL COMMENT '用户组',
`manager` int(11) default NULL COMMENT '渠道经理',
`accounttype` tinyint(2) default NULL COMMENT '账户属性',
`realname` varchar(50) default NULL COMMENT '真实姓名',
`telephone` bigint(12) default NULL COMMENT '固定电话',
`mobilephone` bigint(12) default NULL COMMENT '移动电话',
`qq` int(11) default NULL COMMENT 'QQ号码',
`company_name` varchar(100) default NULL COMMENT '公司名称',
`company_simple_name` varchar(20) default NULL COMMENT '公司简称',
`company_address` varchar(100) default NULL COMMENT '通信地址',
`company_zipcode` int(7) default NULL COMMENT '邮政编码',
`bank_name` varchar(100) default NULL COMMENT '开户银行',
`bank_address` varchar(100) default NULL COMMENT '开户银行地址',
`bank_account` varchar(50) default NULL COMMENT '银行账户',
`bank_username` varchar(50) default NULL COMMENT '开户名称',
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;


我现在想通过联表方式 取出所有app表中数据及uid,manager,channel_id所对应的user表中的username或者realname

请问该怎么实现呢,或者说表设计要怎么改呢
...全文
223 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
benluobo 2014-03-31
  • 打赏
  • 举报
回复
select ch_app.*, A.username ,A.realname , B.username,B.realname,C.username,C.realname from ch_app left join ch_user A on ch_app.uid = A.uid left join ch_user B on ch_app.manager = B.uid left join ch_user C on ch_app.channel_Id = C.uid
yi890410 2014-03-31
  • 打赏
  • 举报
回复
引用 5 楼 benluobobo 的回复:
有个逗号的问题,下面这个可以,查询出来的最后六个字段分别是你要的,你可以在查询语句中给你需要的列别名 select ch_app.*, A.username ,A.realname , B.username,B.realname,C.username,C.realname from ch_app , ch_user A, ch_user B , ch_user C where ch_app.uid = A.uid and ch_app.manager = B.uid and ch_app.channel_Id = C.uid
sql是可以执行的,但是这里是用的where,奥斯不太合适,我觉得应该是以联表方式比较好,要有一个主表,这样能确保取出来的数据不为空
benluobo 2014-03-31
  • 打赏
  • 举报
回复
有个逗号的问题,下面这个可以,查询出来的最后六个字段分别是你要的,你可以在查询语句中给你需要的列别名 select ch_app.*, A.username ,A.realname , B.username,B.realname,C.username,C.realname from ch_app , ch_user A, ch_user B , ch_user C where ch_app.uid = A.uid and ch_app.manager = B.uid and ch_app.channel_Id = C.uid
yi890410 2014-03-31
  • 打赏
  • 举报
回复
引用 3 楼 benluobobo 的回复:
select ch_app.*, A.username,A.realname B.username,B.realname,C.username,C.realname from ch_app , ch_user A, ch_user B , ch_user C, where ch_app.uid = A.uid and ch_app.manager = B.uid and ch_app.channelId = C.uid
你确定行的通嘛,我没执行成功
benluobo 2014-03-31
  • 打赏
  • 举报
回复
select ch_app.*, A.username,A.realname B.username,B.realname,C.username,C.realname from ch_app , ch_user A, ch_user B , ch_user C, where ch_app.uid = A.uid and ch_app.manager = B.uid and ch_app.channelId = C.uid
yi890410 2014-03-31
  • 打赏
  • 举报
回复
引用 1 楼 benluobobo 的回复:
简单的left join应该可以满足你的需求 select ch_app.*, ch_user.username,ch_user.realname from ch_app left join ch_app where ch_app.uid = ch_app.uid
谢谢你的回答,我的问题是如何能同时取出三个uid所对应的用户名(uid,manager,channel_id 这里是对应的三个用户id
benluobo 2014-03-31
  • 打赏
  • 举报
回复
简单的left join应该可以满足你的需求 select ch_app.*, ch_user.username,ch_user.realname from ch_app left join ch_app where ch_app.uid = ch_app.uid
yi890410 2014-03-31
  • 打赏
  • 举报
回复
引用 7 楼 benluobobo 的回复:
select ch_app.*, A.username ,A.realname , B.username,B.realname,C.username,C.realname from ch_app left join ch_user A on ch_app.uid = A.uid left join ch_user B on ch_app.manager = B.uid left join ch_user C on ch_app.channel_Id = C.uid
非常好,谢谢你

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧