使用INNER JOIN后搜索SQL查询

时间:2015-01-27 11:24:22

标签: mysql sql join

在没有搜索的情况下显示表格时,它可以完美打印,当添加where查询(在其他搜索表中没有包含内部连接时工作正常)时会产生语法错误。这是代码:

SELECT Date_entered, photo1, photo2, UserName, reserveName, species FROM Plant_Reserves 
                        INNER JOIN Plant_Species ON Plant_Reserves.plantID = Plant_Species.plantID 
                        INNER JOIN reserves ON Plant_Reserves.reserveID = reserves.reserveID
                        ORDER BY UserName WHERE UserName LIKE '%$search%'

以下是错误的副本:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行'WHERE UserName LIKE'%zz%'附近使用正确的语法

1 个答案:

答案 0 :(得分:2)

where出现在order by

之前
SELECT Date_entered, photo1, photo2, UserName, reserveName, species 
FROM Plant_Reserves 
INNER JOIN Plant_Species ON Plant_Reserves.plantID = Plant_Species.plantID 
INNER JOIN reserves ON Plant_Reserves.reserveID = reserves.reserveID
WHERE UserName LIKE '%$search%'
ORDER BY UserName 

关键字的定义顺序是

select
from 
join
where
group by
having
order by
limit