mysql连接不相关的表
我有两张桌子:mysql连接不相关的表,mysql,sql,Mysql,Sql,我有两张桌子: create table words(id int(3) not null primary key auto_increment, name varchar(10) not null); insert into words values(null, 'rock'); insert into words values(null, 'rick'); insert into words values(null, 'red'); insert into words values(nul
create table words(id int(3) not null primary key auto_increment, name varchar(10) not null);
insert into words values(null, 'rock');
insert into words values(null, 'rick');
insert into words values(null, 'red');
insert into words values(null, 'black');
create table letters(id int(3) not null primary key auto_increment, name char(1) not null, lang char(2) not null);
insert into letters values(null, 'A', 'en');
insert into letters values(null, 'B', 'en');
insert into letters values(null, 'C', 'es');
现在,要获取所有以“r”开头的单词,我需要:
从名称中选择*,如“r%”
如果我想同时得到一个包含所有字母的列表,其中lang='en',那么查询会是什么样子?
我尝试过使用union,但似乎您只能将其用于具有相同列的表。
对于结果列表中的每一行,我希望获得id(来自words)、name(来自words)和符合条件lang='en'的所有字母的串联列表。
我想我需要使用join,但我真的不知道它会是什么样子。有人能帮我吗
SELECT w.id, w.name,
(
SELECT GROUP_CONCAT(name ORDER BY name SEPARATOR ',')
FROM letters
WHERE lang = 'en'
)
FROM words
请注意,每个记录的信件列表都是相同的。你真的需要它在数据集中吗?您可能需要考虑在单独的查询中检索它,并在客户端存储。