mysql-链接到另一个表以使用内部联接获取名称
作者:互联网
以下代码显示了一个项目的最大日期,并且一切正常.
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的priceing.region列.我已经尝试了通常的where子句来联接表并显示描述性名称,但是它破坏了它.
有人可以帮忙吗?
谢谢,
约翰
解决方法:
您必须从地区表中选择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
应该管用.
标签:inner-join,mysql,database 来源: https://codeday.me/bug/20191026/1934694.html