Mysql 如何在SQL表中的两个数据之间进行选择?
我有两张这样的桌子:Mysql 如何在SQL表中的两个数据之间进行选择?,mysql,Mysql,我有两张这样的桌子: Table 1 ------- ID score Table 2 ------- ID score_from score_to 如果我在表1中根据表2中的score_from和score_to得到分数为12,我如何从表2中获得ID Contents of Table1: ---------------------- ID |Score | ---------------------- 1 |12 | ---------
Table 1
-------
ID
score
Table 2
-------
ID
score_from
score_to
如果我在表1中根据表2中的score_from和score_to得到分数为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 |
------------------------------
如果我有表1中的分数为12,我如何编写查询以在表2中获得ID为1?试试这个
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)
select t2.id from table t1 ,table t2
where t1.score between t2.score_from and t2.score_to;