我有两张桌子:
第一个表格是tbl_ccompany
db_id db_cname
1 xxx
2 yyy
第二张表tbl_marketing
db_mid db_name db_ccompany
1 ggg 1,2
在tbl_marketing
db_ccompany中包含第一个表的id
我想选择要打印的db_cname 输出将是这样的
1 ggg xxx,yyy
我在这两个表之间尝试左连接,但只有我收到第一个名字
select
marketing.*,
ccompany.db_cname
from tbl_marketing as marketing
left join tbl_ccompany as ccompany
on
marketing.db_ccompany=ccompany.db_id
我如何加入表格并获得我想要的输出?
答案 0 :(得分:0)
SELECT db_mid,db_name,GROUP_CONCAT(db_cname) FROM tbl_marketing LEFT JOIN tbl_ccompany on db_id=db_ccompany WHERE db_ccompany in(1,2)
答案 1 :(得分:0)
我希望这会有所帮助。
数据库结构:
create table tbl_ccompany (
db_id int,
db_cname text );
insert into tbl_ccompany values (1,'xxx');
insert into tbl_ccompany values (2,'yyy');
create table tbl_marketing (
db_mid int ,
db_name text,
db_ccompany text
);
insert into tbl_marketing values (1,'ggg','1,2');
查询:
select db_mid, db_name, group_concat(db_cname)
from tbl_ccompany tc , tbl_marketing tm
where find_in_set(tc.db_id,tm.db_ccompany)
参考: http://amitbrothers.blogspot.in/2014/03/mysql-split-comma-separated-list-into.html