-1

hi in mysql how can i search my records for customername that only start with c or g ? at the same time. i have tried the following query's

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]%';

but cant get these strings to only show the customernames starting with c and g at the same time in one query

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.

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

works and shows only g names.

but i need them to show at the same time. please help.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
HaKDMoDz
  • 37
  • 2
  • 10

2 Answers2

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

UPDATE: Although Edrich's answer is on the right track, it might not work. Even if it does, it's good practice anyway to put brackets around OR and AND statements to prevent confusion with the server. See this answer for more information.

Community
  • 1
  • 1
Neguido
  • 235
  • 2
  • 13
  • Nope, there's no need to put brackets because there are only 2 condition, of course if the query needs AND operator that's the time that I will put brackets. – Edrich Nov 22 '14 at 01:14
  • @Edrich But, 'good practice' like I said. You should always make habits of things like just in case. – Neguido Nov 22 '14 at 01:16
  • @Edrich I wasn't trying to make your answer look bad in any way, just felt the need to mention it. I've run into many problems because of not getting used to things like this early on. – Neguido Nov 22 '14 at 01:18
  • Like I said it defends on the condition, I know when to use brackets. I'm pretty sure my query should work with no problems/ error – Edrich Nov 22 '14 at 01:20
  • @Edrich Yea, it probably would work now that I think of it. But then again (and this is what I'm trying to say), in the beautiful language of AS3 you don't have to end each line with a semicolon. Should people still do it? Definitely. – Neguido Nov 22 '14 at 01:22
  • OH, I'm surprise why you sensitive with the semi colom ";", Of course people still do it and I'm sure you are using it. – Edrich Nov 22 '14 at 01:26
  • @Edrich Any experienced AS3 programmer will tell you that you SHOULD use a semicolon at the end of each line, regardless of the fact it's not required. Even certain tags in HTML don't NEED to be closed, but definitely should be (and always are) in the name of good practice. – Neguido Nov 22 '14 at 01:32
  • First, I have no idea about AS3. Second, your earlier comment said that I don't have to end each line with a semicolon then after you told that I SHOULD use a semicolon at the end of each line? Therefore I want to end this conversation. I really don't know what your point is, you insist some things that is not related to the post. Bye – Edrich Nov 22 '14 at 01:39
  • @Edrich What I meant by my example is that, AS3 (or ActionScript 3 - the language for programming flash applications) doesn't explicitly require that you use semicolons on the end of each line. And my HTML example was more than valid. I don't see why you don't understand what I'm trying to say here... [Here you go](http://en.wikipedia.org/wiki/Best_coding_practices). – Neguido Nov 22 '14 at 01:46
0

Try this

SELECT customerName FROM Customers WHERE customerName LIKE 'c%' OR customerName like 'g%';
Edrich
  • 232
  • 2
  • 8