给出以下表结构:我需要在人类可读的平面文件中获取记录:
CREATE TABLE `formsfiles`.`Teams` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Teams` (`Name`) VALUES ('Sharks');
INSERT INTO `Teams` (`Name`) VALUES ('Jets');
INSERT INTO `Teams` (`Name`) VALUES ('Fish');
INSERT INTO `Teams` (`Name`) VALUES ('Dodgers');
INSERT INTO `Teams` (`Name`) VALUES ('Pigs');
CREATE TABLE `Players` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
`Team_ID` INT NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jim', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tom', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Harry', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Dave', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tim', '3');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Trey', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jay', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Steve', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Ziggy', '5');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Chris', '4');
我想要玩家表格的输出:
ID Name Team_ID Team
1 Jim 1 Sharks
2 Tom 1 Sharks
3 Harry 2 Jets
4 Dave 2 Jets
我认为这是一个简单的连接,但不是一个SQL人我踩着他的语法,似乎无法得到它。 (请原谅问题的基本性质)
答案 0 :(得分:1)
select p.ID, p.Name, t.ID as Team_ID, t.Name as Team
from formsfiles.Teams t inner join Players p on p.Team_ID = t.ID
答案 1 :(得分:1)
基本上这是你追求的问题:
dat <- structure(list(TEAM1 = c("A", "I", "P", "S", "S", "W", "A", "A",
"E"), TEAM2 = c("P", "S", "S", "I", "P", "P", "E", "S", "A"),
WINNER = c("A", "I", "S", "I", "P", "W", "A", "S", "E")),
.Names = c("TEAM1",
"TEAM2", "WINNER"), class = "data.frame", row.names = c(NA, -9L))
但是你应该真正阅读一些关于连接和sql的内容,这是非常有用的东西。
答案 2 :(得分:1)
// returns all elements from arr1, that doesn't have matching ID in arr2
function except(arr1, arr2, propertyName='ID') {
let exceptIds = new Set(arr2.map(el=>el[propertyName])); // get set of all IDs in array2
return arr1.filter(el=>!exceptIds.has(el[propertyName]));
}