如何从表1中获取值,在表2中进行搜索以及找到后-获取第一个值的行值?

时间:2019-03-05 05:39:40

标签: php mysql

我有table1,这里有id的{​​{1}} s数据-看起来像这样:(我不知道如何在这里创建表)

table2

所以我想从id | word1 | word2 1 | 5 | 7 获取word1word2的值,在table1中搜索它们,然后在{ {1}}。这是我的工作:

table2

什么也没发生。

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么以下是示例数据:

id,word1_id,word2_id

CREATE TABLE IF NOT EXISTS id (`id` INT,`w1` INT,`w2` INT
);
INSERT INTO id VALUES
(1,1,5),
(2,2,4),
(3,8,3),
(4,6,2),
(5,7,1),
(6,4,9),
(7,5,7),
(8,2,6),
(9,3,8);`

单词表

CREATE TABLE IF NOT EXISTS word (
    `w_id` INT,
    `word` VARCHAR(5) CHARACTER SET utf8
);
INSERT INTO word VALUES
(1,'one'),
(2,'two'),
(3,'three'),
(4,'four'),
(5,'five'),
(6,'six '),
(7,'seven'),
(8,'eight'),
(9,'nine');`

这是答案:

select id.w1,id.w2,w1.word,w2.word from id
join word w1 on w1.w_id = id.w1
join word w2 on w2.w_id = id.w2;

7   1   seven   one
6   2   six     two
8   3   eight   three
2   4   two     four
1   5   one     five
2   6   two     six 
5   7   five    seven
3   8   three   eight
4   9   four    nine

让我知道是否可行!!