我有这两张表。我会尽可能简单:
Sample value in main_table:
id = 1
Sample value in details_table:
id = 1
type = book
name = harry potter
id = 1
type = paper
name = post it
我需要的结果是使用这两种类型的名称获取 id。
id book paper
1 harry potter post it
这可能吗?
最佳答案
只需加入两次明细表:
Select m.id, d1.name as book, d2.name as paper from main_table m
join Details_table d1 on m.id =d1.id and d1.type = 'book'
join Details_table d2 on m.id =d2.id and d2.type = 'paper'
关于MySQL一对多关系单次查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39503447/