MySQL查询中的一些INNER JOIN

时间:2013-08-04 08:30:03

标签: mysql sql

我有下一个MySQL表:

|provincias|  
| id          | |provincia|   

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

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

我做下一个MySQL查询(EDITED):

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;

但是我得到了下一个错误(EDITED):

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

2 个答案:

答案 0 :(得分: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表格)

答案 1 :(得分: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;