CREATE TABLE itemmast (
cid INTEGER,
description varchar(30),
defaultprice DOUBLE);
INSERT INTO itemmast VALUES (1, 'Item 1', '0');
INSERT INTO itemmast VALUES (2, 'Item 2', '8');
INSERT INTO itemmast VALUES (3, 'Item 3', '5.5');
INSERT INTO itemmast VALUES (4, 'Item 4', '0');
INSERT INTO itemmast VALUES (5, 'Item 5', '59');
CREATE TABLE specialprice (
cid INTEGER,
username varchar(30),
newprice DOUBLE);
INSERT INTO specialprice VALUES (4, 'UserS', '10');
INSERT INTO specialprice VALUES (2, 'UserX', '115');
我想为特定用户获取 [newprice] 的值,以防万一它在 [specialprice] 表中,否则从 [itemmast] 表中获取 [defaultprice]
到目前为止,我有这个但没有按预期工作。
SELECT itemmast.*,newprice,
CASE specialprice.newprice
WHEN NULL
THEN itemmast.defaultprice
ELSE
specialprice.newprice
END AS itemprice
FROM itemmast
LEFT JOIN specialprice ON specialprice.cid = itemmast.cid
where itemmast.cid = '1' and username= 'UserS'