创建比较条件 / 使用单字符通配符 |
图: 查询
SELECT stock_num, manu_code, description, unit_price FROM stock WHERE description LIKE 'bicycle%' ORDER BY description, manu_code; SELECT stock_num, manu_code, description, unit_price FROM stock WHERE description MATCHES 'bicycle*' ORDER BY description, manu_code;
图: 查询结果
stock_num manu_code description unit_price 102 PRC bicycle brakes $480.00 102 SHM bicycle brakes $220.00 114 PRC bicycle gloves $120.00 107 PRC bicycle saddle $70.00 106 PRC bicycle stem $23.00 101 PRC bicycle tires $88.00 101 SHM bicycle tires $68.00 105 PRC bicycle wheels $53.00 105 SHM bicycle wheels $80.00
比较 'bicycle%' 或 'bicycle*' 指定字符 bicycle 后跟零个字符或任何字符序列。它与 bicycle stem 匹配,而 stem 与通配符匹配。如果具有该描述的行存在,那么它只与字符 bicycle 匹配。
图: 查询
SELECT stock_num, manu_code, description, unit_price FROM stock WHERE description LIKE 'bicycle%' AND manu_code NOT LIKE 'PRC' ORDER BY description, manu_code;
图: 查询结果
stock_num manu_code description unit_price 102 SHM bicycle brakes $220.00 101 SHM bicycle tires $68.00 105 SHM bicycle wheels $80.00
当从大型表中进行选择并在比较字符串中使用词首通配符时(如 '%cycle'),查询通常需要较长时间来执行。由于不能使用索引,所以搜索每一行。