简体   繁体   中英

SQL syntax error near WHERE clause

Can anyone tell me what's wrong in this query?

<cfquery name="activesurveys">
      SELECT surveys.id,
             surveys.name,
             surveys.description,
      WHERE  surveys.active= 1
        AND  surveys.showinpubliclist= 1
     FROM 
             surveys
</cfquery>
<cfreturn activesurveys>

When I execute this code it gives this error:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE surveys.active= 1 AND surveys.showinpubliclist= 1' at line 4"

I think your query should be:

SELECT surveys.id,
    surveys.name,
    surveys.description
FROM surveys
WHERE surveys.active= 1
    AND surveys.showinpubliclist= 1

Basically, there's a , after the last field you want to select, which is wrong, and also there's the fact that FROM goes after SELECT and before WHERE ...

You need to re-order your statement a bit. The FROM needs to be before the WHERE . And you have an extra comma after the last column in your SELECT . Should look something like this:

<cfquery name="activesurveys">
    SELECT surveys.id,
           surveys.name,
           surveys.description
    FROM  surveys
    WHERE surveys.active = 1
      AND surveys.showinpubliclist = 1
</cfquery>
<cfreturn activesurveys>

I don't know ColdFusion but the SQL Syntax is

SELECT surveys.id,
       surveys.name,
       surveys.description
FROM 
       surveys
WHERE  surveys.active= 1
       AND surveys.showinpubliclist= 1

Kill the comma from the end of the last column name "surveys.description," and move the FROM statement before WHERE clause. Like this

SELECT colname, colname, colname
FROM tablename

WHERE condition...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM