简体   繁体   中英

Mysql Compare two columns

I've a database table with the following columns:
id_product, qty, total

I want to select all products where qty < total.
This is what I'm using right now but it doesn't work:

SELECT * FROM tb_products WHERE id_product='$id' AND qty < total;

i guess you looking for this query:

    SELECT * FROM tb_products WHERE   qty < total;

you dont have to look for the id_product

small demo

Marc B was right:

perhaps you're using varchars for your qty/total fields, in which case qty=6 and total=50 would actually give you your symptoms. comparing numbers as strings means 6 is greater than 50 because 6 > 5 = true.

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