如何通过 mysql left join 获取名字两次
how to get name twice via mysql left join
我有两张桌子。一个用于会员信息,另一个用于董事会信息。
我想用名称显示 'writer' 和 'viewer' 但我只能通过 'LEFT JOIN'
显示其中一个
SELECT * FROM `table_a`
idx name
1 Admin
2 Superman
3 Ironman
4 Batman
SELECT * FROM `table_bbs`
idx writer title viewer
1 1 Hi All 0
2 1 hello 2
3 2 My name is 3
4 3 Do not click 4
SELECT bbs.writer, a.name, bbs.title, bbs.viewer FROM `table_bbs` AS bbs
LEFT JOIN table_a AS a ON a.idx = bbs.writer;
writer name title viewer
1 Admin Hi All 0
1 Admin hello 2
2 Superman My name is 3
3 Ironman Do not click 4
如何获得查看者的姓名?
如果观众数据来自 table_a
SELECT bbs.writer, a.name as writer_name, bbs.title, bbs.viewer, b.name as viewer_name FROM `table_bbs` AS bbs
LEFT JOIN table_a AS a ON a.idx = bbs.writer
LEFT JOIN table_a AS b ON b.idx = bbs.viewer
我有两张桌子。一个用于会员信息,另一个用于董事会信息。 我想用名称显示 'writer' 和 'viewer' 但我只能通过 'LEFT JOIN'
显示其中一个SELECT * FROM `table_a`
idx name
1 Admin
2 Superman
3 Ironman
4 Batman
SELECT * FROM `table_bbs`
idx writer title viewer
1 1 Hi All 0
2 1 hello 2
3 2 My name is 3
4 3 Do not click 4
SELECT bbs.writer, a.name, bbs.title, bbs.viewer FROM `table_bbs` AS bbs
LEFT JOIN table_a AS a ON a.idx = bbs.writer;
writer name title viewer
1 Admin Hi All 0
1 Admin hello 2
2 Superman My name is 3
3 Ironman Do not click 4
如何获得查看者的姓名?
如果观众数据来自 table_a
SELECT bbs.writer, a.name as writer_name, bbs.title, bbs.viewer, b.name as viewer_name FROM `table_bbs` AS bbs
LEFT JOIN table_a AS a ON a.idx = bbs.writer
LEFT JOIN table_a AS b ON b.idx = bbs.viewer