如何比较mysql中2个表中的值

时间:2018-06-05 08:04:06

标签: mysql

我有2个表格,其值如下:

A : 
Product   |   Number (int)  |
Paper          10
Pen            25
Pencil         5
Eraser         6

--------------------------------------------------
B : 
Product   |   Number (int)  |
Paper          8
Pen            30
Pencil         12
Eraser         0

现在,我想比较表" B"用" A"并在" B"中找到产品该数字大于" A"

(在这种情况下会找到" Pen"" Pencil"来自" B")

1 个答案:

答案 0 :(得分:2)

只需使用如下所示的连接:

SELECT B.*
FROM A JOIN B
ON A.Product=B.Product AND A.`Number`<B.`Number`;

OR

SELECT *
FROM B 
WHERE EXISTS (SELECT 1 FROM A WHERE A.Product=B.Product AND A.`Number`<B.`Number`);