以下代码显示了某个项目的最长日期,并且一切正常。
SELECT
pricing_id, pricing.field, pricing.region, price, max_date
FROM
pricing
INNER JOIN
(SELECT
field, MAX(end_date) AS 'max_date'
FROM
pricing, regions
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
ORDER BY region, pricing.field
我正在尝试从regions.region_name中提取区域名称,以替换仅显示ID的pricing.region列。我已经尝试了通常的where子句来连接表并显示描述性名称,但它会破坏它。
有人可以帮忙吗?
谢谢,
约翰
答案 0 :(得分:3)
您必须从区域表中选择region_name
类似
SELECT
pricing_id, pricing.field, tmptable.region_name , price, max_date
FROM
pricing
INNER JOIN
(SELECT
region_name , field, MAX(end_date) AS 'max_date'
FROM
pricing, regions
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
ORDER BY region, pricing.field
应该有用。
答案 1 :(得分:1)
假设您的表在设置中类似,这应该有用。我假设你不想尝试在子选择中找到它,这似乎比需要更多的工作。相反,我将其添加为LEFT JOIN并将剩余的查询更改为匹配。
SELECT
pricing_id, pricing.field, r.region_name, price, max_date
FROM
pricing
INNER JOIN
(SELECT
field, MAX(end_date) AS 'max_date'
FROM
pricing
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
LEFT JOIN regions r ON r.id = pricing.region
ORDER BY r.region_name, pricing.field