我有2张这样的表:
Table 1
-------
ID
score
Table 2
-------
ID
score_from
score_to
如果我根据表2中的score_from和score_to在表1中得分='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获得得分='12',我如何编写查询以获得表2中的ID ='1'?
答案 0 :(得分: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)
或者如果您只想要ID
SELECT b.`ID`
FROM table1 a, table2 b
WHERE (a.score BETWEEN b.score_from AND b.score_to) AND
(a.score = 12)
答案 1 :(得分:0)
select t2.id from table t1 ,table t2
where t1.score between t2.score_from and t2.score_to;