我有2个选择返回此字段。 选择1:
+-----------+---------+-------+
| serviceID | modelID | price |
+-----------+---------+-------+
| 1 | 100 | 0 |
+-----------+---------+-------+
| 2 | 100 | 0 |
+-----------+---------+-------+
| 1 | 200 | 0 |
+-----------+---------+-------+
| 2 | 200 | 0 |
+-----------+---------+-------+
SELECT 2
+-----------+---------+-------+
| serviceID | modelID | price |
+-----------+---------+-------+
| 1 | 100 | 499 |
+-----------+---------+-------+
我需要这样的结果:
+-----------+---------+-------+
| serviceID | modelID | price |
+-----------+---------+-------+
| 1 | 100 | 499 |
+-----------+---------+-------+
| 2 | 100 | 0 |
+-----------+---------+-------+
| 1 | 200 | 0 |
+-----------+---------+-------+
| 2 | 200 | 0 |
+-----------+---------+-------+
即。全部来自选择1,但如果选择2有fld替换为select1
答案 0 :(得分:0)
SELECT A.serviceID, A.modelID, IF(B.price IS NULL, A.price, B.price) AS price
FROM table1 A LEFT JOIN table2 B ON
A.serviceID = B.serviceID AND A.modelID= B.modelID
答案 1 :(得分:0)
select * from (
( select 2 here ) union ( select 1 here ) ) as r group by serviceID, modelID
这对我有用。可能它会帮助某人。