简体   繁体   中英

What is the best way to select records from value/unit table?

I have hard_drives table with columns producent, size, unit

samsung|256|GB
caviar |850|MB
sandisc|512|GB
seagate|2  |TB

I'd like to create queries for selecting hard drives with size > 128GB , size < 1000MB , size < 1TB etc.

Would you add new column like size_in_bytes , or create some conversion table like storage_units with columns unit, size_in_bytes and use it for calculations, or maybe there is some better way to perform such queries?

You should use a common unit, probably MB, for storing all disk sizes. As a workaround, you can use a CASE expression in the WHERE clause:

SELECT *
FROM yourTable
WHERE
    CASE WHEN unit = 'MB' THEN size
         WHEN unit = 'GB' THEN 1000*size
         WHEN unit = 'TB' THEN 1000*1000*size END > 128*1000   -- size > 128GB

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