如何在数据库中搜索以c或g开头的名字?

时间:2014-11-22 00:56:17

标签: mysql

嗨,在mysql中如何在我的记录中搜索只能以c或g开头的客户名?同时。我尝试过以下查询

SELECT customerName FROM Customers WHERE customerName LIKE 'c%' or like 'g%'; SELECT customerName FROM Customers WHERE customerName LIKE 'c%' or text like 'g%'; SELECT customerName FROM Customers WHERE customerName LIKE '[c]%' or like '[g]%';

但是无法将这些字符串仅显示在一个查询中同时以c和g开头的客户名称

mysql> select * from customers; +------------+--------------+--------------+----------------+---------+ | customerID | customerName | customerCity | customerRating | SalesID | +------------+--------------+--------------+----------------+---------+ | 1 | Hoffman | London | 100 | 1 | | 2 | Giovanni | Rome | 200 | 5 | | 3 | Liu | San Jose | 200 | 2 | | 4 | Grass | Berlin | 300 | 2 | | 5 | Clemens | London | NULL | 1 | | 6 | Cisneros | San Jose | 300 | 4 | | 7 | Pereira | Rome | 100 | 3 | +------------+--------------+--------------+----------------+---------+



SELECT customerName FROM Customers WHERE customerName LIKE 'c%';

works and shows only c names.

工作并只显示g名称。

但我需要他们同时展示。请帮忙。

2 个答案:

答案 0 :(得分:0)

SELECT customerName FROM Customers WHERE (customerName LIKE 'c%' OR customerName LIKE 'g%')

更新:虽然Edrich的答案是正确的,但它可能无效。即使它确实如此,但最好将括号括在ORAND语句周围,以防止与服务器混淆。有关详细信息,请参阅this答案。

答案 1 :(得分:0)

试试这个

SELECT customerName FROM Customers WHERE customerName LIKE 'c%' OR customerName like 'g%';