简体   繁体   中英

What's wrong with this case SQL query?

maybe you can help, I'm using this case query, and I'm trying to do mysql weighting with it. What's wrong with it?

  SELECT *
    FROM cronjob_reloaded
   WHERE site IN ('site1.com', 'site2.com')
ORDER BY (CASE site
            WHEN 'site1.com' THEN 0.2
            WHEN 'site2.com' THEN 0.8    ) * RAND( ) DESC
   LIMIT 0 , 30 

MySQL said: Documentation

1064 - 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 ') * RAND() DESC LIMIT 0, 30' at line 4

Here is the right syntax of CASE

SELECT *
FROM cronjob_reloaded
WHERE site IN ('site1.com', 'site2.com' )
ORDER BY (
  CASE 
    WHEN site = 'site1.com' THEN 0.2
    WHEN site = 'site2.com' THEN 0.8 
  END
) * RAND( ) DESC
LIMIT 0 , 30 

CASE must be closed with END:

CASE site
WHEN 'site1.com'
THEN 0.2
WHEN 'site2.com'
THEN 0.8
END -- Added this END to your query's CASE

One possible issue is that you have not used END CASE.

Change:

CASE site
WHEN 'site1.com'
THEN 0.2
WHEN 'site2.com'
THEN 0.8
)

To:

CASE site
WHEN 'site1.com'
THEN 0.2
WHEN 'site2.com'
THEN 0.8
END CASE

Refer to: MySQL site

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