SELECT *, p.name AS name, p.image, p.price,
( SELECT ps.price FROM product_special ps WHERE p.id = ps.id
AND ps.date < NOW() ORDER BY ps.priority ASC, LIMIT 1) AS special_price,
( SELECT ps.date FROM product_special ps WHERE p.id = ps.id
AND ps.date < NOW() ORDER BY ps.priority ASC, LIMIT 1) AS date
FROM product p LEFT JOIN product_special ps ON (p.id = ps.id)
As you can see I'm repeating the same subquery just to get another column out. I'm wondering is there a better way of doing this?
id
, the Primary Keys in both tables? – ypercube Apr 2 '12 at 23:33pd.name
should raise an error. You have not definedpd
anywhere. – ypercube Apr 2 '12 at 23:34(product_special.id, product_special.priority)
unique ? – a1ex07 Apr 3 '12 at 1:51