2 回答
TA贡献1874条经验 获得超12个赞
如果您只能从文件中选择车辆,那么您需要执行以下查询
SELECT f.name
FROM tags_files tf INNER JOIN files f ON tf.id_file = f.id
WHERE id_tag_main = (SELECT id_tag_main
FROM tags_files
WHERE id_file = (SELECT id FROM files WHERE name = 'vehicles' ));
CREATE TABLE tags_files (
`id` int,
`id_tag` int,
`id_file` int,
`id_tag_main` int
);
INSERT INTO tags_files
(`id`, `id_tag`, `id_file`, `id_tag_main`)
VALUES
('1', '1', '1', '3'),
('2', NULL, '2', '2'),
('3 ', NULL, '3', '2'),
('4', NULL, '4', '2');
✓
✓
CREATE TABLE files (
`id` int,
`name` VARCHAR(14)
);
INSERT INTO files
(`id`, `name`)
VALUES
('1', 'city'),
('2', 'vehicles'),
('3', 'weather'),
('4', 'gifts');
✓
✓
CREATE TABLE tags (
`id` int,
`name` VARCHAR(11)
);
INSERT INTO tags
(`id`, `name`)
VALUES
('1', 'one'),
('2', 'two'),
('3', 'three');
✓
✓
SELECT f.name
FROM tags_files tf INNER JOIN files f ON tf.id_file = f.id
WHERE id_tag_main = (SELECT id_tag_main
FROM tags_files
WHERE id_file = (SELECT id FROM files WHERE name = 'vehicles' ));
| 姓名 |
| :-------- |
| 车辆 |
| 天气|
| 礼品|
TA贡献1808条经验 获得超4个赞
也许是这个?
select *
from files as f1
left join tags_files on tags_files.id_tag_main = f1.id
left join files as f2 on tags_files.id_file = f2.id
where f1.name = 'vehicles'
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报