在sql中使用less than运算符比较两个表

时间:2016-03-20 06:12:45

标签: mysql

我有两张桌子。即TEAM1和TEAM2

  TEAM 1                     TEAM 2
+--------+-------+      +--------+-------+
| NAME   | VALUE |      | NAME   | VALUE |
+--------+-------+      +--------+-------+
| APPLE  | 5     |      | APPLE  | 4     |
| ORANGE | 8     |      | ORANGE | 12    |
| GRAPES | 10    |      | GRAPES | 12    |
| PEAR   | 12    |      | PEAR   | 9     |
+--------+-------+      +--------+-------+

我正在创建一个sql查询,该查询将返回Team2 Name和Value,其值(小于)小于Team1。示例结果如下所示:

Result
+--------+-------+
| NAME   | VALUE |
+--------+-------+
| APPLE  | 4     |
| PEAR   | 9     |
+--------+-------+

我应该使用什么SQL查询?提前致谢

3 个答案:

答案 0 :(得分:1)

这应该可以为您提供所需的结果。

SELECT T2.NAME, T2.VALUE
  FROM TEAM2 as T2 --Base table is Team2
 INNER JOIN TEAM1 as T1 ON T1.NAME = T2.NAME --Join both the tables
 WHERE T2.VALUE < T1.VALUE --Filter condition

您可以详细了解SQL Joins from w3Schools

答案 1 :(得分:1)

select t2.* from TEAM1 t1,TEAM2 t2 where t1.NAME = t2.NAME and t2.VALUE < t1.VALUE

答案 2 :(得分:0)

我认为这就是你想要的:

Select b.Name, b.Value
From Team1 as a, team2 as b
Where b.Name = a.Name and
b.Value < a.Value