0

美好的一天,我在查询 2 个 mysql 表时遇到问题。

表格1

c_id     c_name
**-----------------**
 1      blah,blah
 2      hey

表 2

m_id    c_id    m_name
-----------------------
 1       1     some_name
 2       1     some-name
 3       1     some/name
 4       1     some.name
 5       2     name.some
 6       2     name-some

我希望显示是这样的:

c_id     c_name       m_name
-----------------------------
 1     blah,blah     some_name
                     some-name
                     some/name
                     some.name
 2     hey           name.some
                     name-some
4

4 回答 4

1
Select a.c_id, a.c_name, 
group_concat(b.m_name SEPARATOR '<br/>') as m_name from table1 a 
left join table2 b on a.c_id = b.c_id
group by a.c_id;

小提琴

使用 m_name 绑定到标签。它将带有换行符

于 2013-07-30T16:34:03.067 回答
1

此查询将准确输出您想要的内容:

select case when first.m_id is null then ' ' else t1.c_id end c_id, 
       case when first.m_id is null then ' ' else t1.c_name end c_name, 
       m_name
from Table1 t1
inner join Table2 t2 on t1.c_id = t2.c_id
left outer join (
    select m_id,t1.c_id, t1.c_name, min(m_name)
    from Table1 t1
    inner join Table2 t2 on t1.c_id = t2.c_id
    group by t1.c_id, t1.c_name) first on t2.m_id = first.m_id  
order by t1.c_id, t2.m_id

请参阅 SQLFIDDLE:http ://www.sqlfiddle.com/#!2/4bab3/16/0

于 2013-07-30T17:06:14.147 回答
0
Select a.c_id, a.c_name, b.m_name from  table1 a left join table2 b on a.c_id  = b.c_id;
于 2013-02-16T11:29:01.937 回答
0

您可以使用 join 将两个表的结果放在一起:

select c_id, c_name, m_name from table1 as x left outer join table2 as y on x.c_id = y.c_id;
于 2013-02-16T11:29:43.387 回答