我有两张表user_inform
和company_info
。
表user_info
:
userid - username
1 ------------> a
2-------------> b
3-------------> c
表company_info
:
companyid - companyname - CreatedBy - ModifiedBy
101 ------------> c1------------------->1---------------------->1
102-------------> c2------------------->2---------------------->3
103-------------> c3------------------->1---------------------->2
如何在表company_info中的CreatedBy和ModifiedBy位置获取用户名,即表user_info中的用户名?
即
companyid - companyname - CreatedBy - ModifiedBy
101----> c1----> a-----> a
102----> c2----> b-----> c
103----> c3----> a-----> b
我试过这个:
select A.companyid, A.companyname, B.username as CreatedBy, ModifiedBy
from company_info A
inner join user_inform B
on (A.CreatedBy = B.userid)
答案 0 :(得分:1)
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
@Component({
selector: 'app-root',
template: `
<h1>{{title}}</h1>
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes">
<span class='badge'>{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name">
</div>
`,
styles: [`
.selected {
background-color: #CFD8DC !important;
color: white;
}
.heroes {
margin: 0 0 2em 0;
list-style-type: none;
padding: 0;
width: 15em;
}
.heroes li {
cursor: pointer;
position: relative;
left: 0;
background-color: #EEE;
margin: .5em;
padding: .3em 0;
height: 1.6em;
border-radius: 4px;
}
.heroes li.selected:hover {
background-color: #BBD8DC !important;
color: white;
}
.heroes li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}
.heroes .text {
position: relative;
top: -3px;
}
.heroes .badge {
display: inline-block;
font-size: small;
color: white;
padding: 0.8em 0.7em 0 0.7em;
background-color: #607D8B;
line-height: 1em;
position: relative;
left: -1px;
top: -4px;
height: 1.8em;
margin-right: .8em;
border-radius: 4px 0 0 4px;
}
`],
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Tour of Heroes';
heroes = HEROES;
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
user_info表两次。第一次获得用户创建,第二次获得用户修改。
LEFT JOIN
(select ci.companyid, ci.companyname, uc.username CreatedBy, um.username ModifiedBy
from company_info ci
left join user_info uc on ci.CreatedBy = uc.userid
left join user_info um on ci.ModifiedBy = um.userid
以防其中一个用户丢失。)
答案 1 :(得分:0)
尝试以下查询:
SELECT *
FROM table1
JOIN User_table T1 ON table1.CreatedBy = T1.userid
LEFT JOIN User_table T2 ON table1.ModifiedBy= T2.userid
答案 2 :(得分:0)
Mansoor(上图)已接近:
SELECT
c.companyid, c.companyname, u_c.username AS CreatedBy, u_m.username AS ModifiedBy
FROM
(company_info c INNER JOIN user_info u_c ON company_info.CreatedBy = u_c.userid)
LEFT JOIN user_info u_m ON company_info.ModifiedBy = u_m.userid