1

我有 2 个这样的表:

Table 1
-------
ID
score

Table 2
-------
ID
score_from
score_to

如果基于表 2 中的 score_from 和 score_to 在 Table1 中有 score='12' ,我如何从表 2 中获取 ID?

Contents of Table1:
----------------------
ID          |Score   |
----------------------
1           |12      |
----------------------
2           |40      |
----------------------

Contents of Table2:
------------------------------
ID       |score_from|score_to|
------------------------------
1        |0         |20      |
------------------------------
2        |21        |40      |
------------------------------

如果我从 table1 获得 score='12',我该如何编写查询以在 table2 中获取 ID='1'?

4

2 回答 2

2

试试这个,

SELECT a.`ID`, a.`Score`, b.`ID`, b.`score_from`, b.`score_to`
FROM   table1 a, table2 b
WHERE  (a.score BETWEEN b.score_from AND b.score_to) AND
        (a.score = 12)

SQLFiddle 演示

或者如果你只想要ID

SELECT b.`ID`
FROM   table1 a, table2 b
WHERE  (a.score BETWEEN b.score_from AND b.score_to) AND
        (a.score = 12)

SQLFiddle 演示

于 2012-09-24T05:27:27.540 回答
0
select t2.id from table t1 ,table t2
where t1.score between t2.score_from and t2.score_to;
于 2012-09-24T05:29:00.723 回答