简体   繁体   中英

SQL - conditions during query

Basically i want through all the 0's on my database and then start going through the 2's.

The query is:

   SELECT IP,Time,HostName
     FROM RoughScan
CASE WHEN EXISTS (DeepScan=0) 
     THEN (DeepScan=0 LIMIT 1) 
     ELSE (DeepScan=2 LIMIT 1)

but i get the error saying:

Unexpected end of CASE expression (near "" at position 0)

Also i am using MariaDB Server.

If you want one row of each type, then use union all :

(SELECT `IP`,`Time`,`HostName`
 FROM `RoughScan`
 WHERE DeepScan = 0
 LIMIT 1
)
UNION ALL
(SELECT `IP`,`Time`,`HostName`
 FROM `RoughScan`
 WHERE DeepScan = 2
 LIMIT 1
);

Note that ORDER BY is usually used with LIMIT .

You need to apply CASE to WHERE like this:

SELECT IP,Time,HostName
FROM RoughScan
WHERE 
  DeepScan = CASE 
    WHEN EXISTS (SELECT 1 FROM RoughScan WHERE DeepScan = 0) THEN 0 
    ELSE 2 
  END
LIMIT 1

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