IN 查询 :SELECT 字段名1, 字段名2, ...... , 字段名n FROM 表名 WHERE 查询条件 IN (范围) ;
mysql> SELECT s_id, f_name, f_price FROM fruits WHERE s_id IN (101,102); +------+------------+---------+ | s_id | f_name | f_price | # 查询s_id为101和102的记录 +------+------------+---------+ | 101 | apple | 5.20 | | 101 | blackberry | 10.20 | | 102 | orange | 11.20 | | 101 | cherry | 3.20 | | 102 | banana | 10.30 | | 102 | grape | 5.30 | +------+------------+---------+
mysql> SELECT s_id, f_name, f_price FROM fruits WHERE s_id NOT IN (101,102); +------+---------+---------+ | s_id | f_name | f_price | # 与IN相反,用来查询不在条件范围内的记录 +------+---------+---------+ | 103 | apricot | 2.20 | | 104 | berry | 7.60 | | 107 | xxxx | 3.60 | | 105 | melon | 8.20 | | 104 | lemon | 6.40 | | 106 | mango | 15.70 | | 105 | xbabay | 2.60 | | 105 | xxtt | 11.60 | | 103 | coconut | 9.20 | | 107 | xbababa | 3.60 | +------+---------+---------+
时间: 2024-11-11 09:14:14