gpt4 book ai didi

mysql - 使用外键从其他表中选择数据?

转载 作者:行者123 更新时间:2023-11-29 17:14:00 25 4
gpt4 key购买 nike

所以我试图从排名表中获取特定member_id 的部落标签。但我不明白该怎么做。我使用 INNER JOIN 命令但没有任何效果。

CREATE TABLE ranks(
rank VARCHAR(8),
tag VARCHAR(18) NOT NULL,
PRIMARY KEY(rank)
);

CREATE TABLE member(
member_id INT UNSIGNED AUTO_INCREMENT,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15),
ign VARCHAR(20) NOT NULL,
rank VARCHAR(8) NOT NULL,
joined_date DATE NOT NULL,
dob DATE,
sex ENUM('M','F') NOT NULL,
country VARCHAR(3) NOT NULL,
PRIMARY KEY(member_id),
FOREIGN KEY(rank) REFERENCES ranks(rank)
);

INSERT INTO ranks VALUES
('Founder','|NoMercy|King'),
('Admin','^1|NoMercy|^7'),
('TmpAdmin','^5|NoMercy|^7'),
('Pro','^1|NoMercy PRO|^7'),
('Member','^4|NoMercy|^7'),
('Banned','BANNED');

INSERT INTO member VALUES
(NULL,'Reznov','NULL','REZ','Member','2017/12/22','1954/02/28','M','RUS'),
(NULL,'Amanda','NULL','S3XXY|G!RL|','Pro','2018/01/05','1992/01/25','F','USA'),
(NULL,'Elmasri','Navathe','INDIAN_Noob _XOX','TmpAdmin','2018/04/02','1960/08/25','M','IND'),
(NULL,'Tony','Silva','Cool KiD','Member','2018/04/26','1988/02/22','M','BR'),
(NULL,'Hashan','NULL','Big Papa','Member','2018/05/08','1996/06/12','M','NZ'),
(NULL,'Emma','Watson','EmmaXXX','Banned','2018/06/10','1985/05/22','F','UK');

我需要为特定成员输出正确的标签。它应该只显示标签。

请帮忙!

最佳答案

下面的查询应该可以工作

SELECT tag FROM ranks INNER JOIN member  ON ranks.rank=member.rank 
where member_id=1;

http://www.sqlfiddle.com/#!9/e90392/1

    tag
^4|NoMercy|^7

关于mysql - 使用外键从其他表中选择数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51731642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com