我有两张tablebelow:-
表a
-------------------------------
| product_id | price |
-------------------------------
| 1 | 2.2000 |
| 2 | 0.0000 |
| 3 | 0.3333 |
表b
-------------------------------
| product_id | price_discount |
-------------------------------
| 1 | 1.8000 |
如何使用select语句选择上述两个表的价格范围?
example:- SELECT * FROM TABLE A WHERE**price or price discount**BETWEEN 0 AND 2;
预期结果将显示由于产品id有折扣价格=1.8(在0到2的价格范围内)。
-------------------------------
| product_id | price |
-------------------------------
| 1 | 2.2000 |
| 2 | 0.0000 |
| 3 | 0.3333 |
请帮忙。谢谢您。
1条答案
按热度按时间ia2d9nvy1#
一个简单的
LEFT JOIN
在这两张table之间就够了。LEFT JOIN
因为两个表之间可以有不匹配的行。编辑:以前的版本做了额外的检查
tb.price_discount IS NOT NULL
; 但它确实不需要,而且BETWEEN..AND
够了。这是在
OR
操作员文件1 OR NULL = 1
```SELECT ta.*
FROM tablea AS ta
LEFT JOIN tableb AS tb
ON ta.product_id = tb.product_id
WHERE ta.price BETWEEN 0 AND 2
OR
tb.price_discount BETWEEN 0 AND 2