Mysql 3个不同的表格(1个相关表格)。当我打电话的时候。传入值被复制
第一桌Mysql 3个不同的表格(1个相关表格)。当我打电话的时候。传入值被复制,mysql,sql,Mysql,Sql,第一桌 a.id | a.name 1 | apple 2 | peace 3 | grape 第二桌 b.id | b.name 1 | yellow 2 | red 3 | green thidr关系表 a.id | b.id 1 | 2 1 | 1 3 | 3 3 | 1 2 | 1 我想看看: ...LIKE '%pe%' and tag 'green'; peace gree
a.id | a.name
1 | apple
2 | peace
3 | grape
第二桌
b.id | b.name
1 | yellow
2 | red
3 | green
thidr关系表
a.id | b.id
1 | 2
1 | 1
3 | 3
3 | 1
2 | 1
我想看看:
...LIKE '%pe%' and tag 'green';
peace green
grape green
这就是我所尝试的:
SELECT *
FROM a
INNER JOIN c ON a.a_id = c.a_id
INNER JOIN b ON c.b_id = b.b_id
WHERE a.a_name LIKE '%pe%'
我相信有更好的方法来形成这个查询,但这似乎能满足您的需要
select f.name as fruit, c.name as color from
fruit_color as fc
join fruit as f on f.id = fc.fruit_id
join color as c on c.id = fc.color_id
where f.name like '%pe%' and c.name like '%green%'
输出:
fruit | color
---------------
grape | green
peace | green
您几乎得到了WHERE子句。现在还可以尝试查询的其余部分。我在您的表中没有看到列
标记
?你是说b.name
?你就快到了。只需添加连接。@waka I从a.a_id=c.a_id上的内部连接c中选择*在c.b_id=b.b_id上的内部连接b,其中a.a_名称类似“%pe%”;我看到值重复1-葡萄绿2-葡萄黄3-和平黄,但我不想重复,只要在where子句中包含颜色,如果您还想包含该颜色。