-3

我有下一个 MySQL 表:

|provincias|  
| id          | |provincia|   

|municipios|
| provincia_id| | id | |municipio|

|**event**|
|municipio_id|
|idEvent|
|name|
|date_begin|
|date_end|               

我执行下一个 MySQL 查询(已编辑):

SELECT idEvent, name 
FROM evento e 
INNER JOIN provincias p ON p.id = m.provincia_id 
INNER JOIN municipios m ON m.id = e.municipio_id 
WHERE  provincia = "Iruña de Oka" AND municipio = "Álava" 
AND DATE(date_begin) BETWEEN DATE('2013-07-23') AND DATE('2013-07-26')
ORDER BY name;

但我得到下一个错误(已编辑):

1054 - Unknown column 'm.provincia_id' in 'on clause'
4

2 回答 2

4

连接语句在WHERE子句之前

SELECT idEvent, name 
FROM event e
INNER JOIN municipio m ON m.id = e.municipio_id 
INNER JOIN provincia p ON p.id = m.provincia_id 
WHERE  provincial = "Iruña de Oka" AND municipio = "Álava"
AND DATE(date_begin) BETWEEN DATE('2013-07-23') AND DATE('2013-07-26')
ORDER BY name;

(而且您混合了非 ANSI 和 ANSI 连接语法,我已将其编辑为仅使用 ANSI 形式)

于 2013-08-04T08:31:50.633 回答
2

from子句中,表在定义之前不能被引用。您在定义之前的m第一个子句中使用。on

简单的解决方案是重新排列子句:

SELECT idEvent, name 
FROM evento e 
INNER JOIN municipios m ON m.id = e.municipio_id
INNER JOIN provincias p ON p.id = m.provincia_id 
WHERE  provincia = "Iruña de Oka" AND municipio = "Álava" 
AND DATE(date_begin) BETWEEN DATE('2013-07-23') AND DATE('2013-07-26')
ORDER BY name;
于 2013-08-04T15:01:03.743 回答