gpt4 book ai didi

mysql - 连接3个表的Mysql查询是什么?

转载 作者:行者123 更新时间:2023-11-29 14:02:42 24 4
gpt4 key购买 nike

 CREATE TABLE `entity1` (
`entity_id` varchar(20) NOT NULL DEFAULT '',
`name` varchar(20) ,
`version` varchar(20) ,
PRIMARY KEY (`entity_id`));


CREATE TABLE `ent_att` (
`att_id` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`valu` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`valu`),
FOREIGN KEY (`att_id`) REFERENCES `entity1` (`entity_id`));

CREATE TABLE `perf` (
`ent_id` varchar(20) ,
`state` varchar(20) ,
`status` varchar(20) ,
`perf_id` varchar(20) ,
FOREIGN KEY (`ent_id`) REFERENCES `entity1` (`entity_id`));

第一个表的主键是其他两个表的外键,我想从所有表中检索entity_id的所有数据。

最佳答案

试试这个:

select et.entity_id,et.name,et.version,
ett.name as ent_att_name,ett.valu,pf.state,pf.perf_id,pf.status from entity1 et
inner join ent_att ett
on et.entity_id=ett.att_id
inner join perf pf
on et.entity_id=pf.ent_id

关于mysql - 连接3个表的Mysql查询是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14830162/

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