Home > Net >  Search same values form different column
Search same values form different column

Time:06-13

I want to search for the same value in a different column, for example in the upload picture. We can see tel1, tel2 and tel3 have the same telephone number. how can I search it form script enter image description here

I have one table only, I want to search a data from the table, example : our customer tel no is 65432100, I need to find this number appear in which order _id. In this case, 65432100 appear in order_id 1,3,5

enter image description here

CodePudding user response:

You can simply use an IN clause.

SELECT order_id, cust_name, tel1, tel2, tel3 
FROM yourtable 
WHERE 65432100 IN (tel1,tel2,tel3)

If the columns are of type varchar, write '65432100' instead of 65432100.

As long as you want to check one number only, this will be the best way. When you want to check multiple numbers or check multiple conditions, it could be necessary to use OR instead.

The above query using OR would be this one:

SELECT order_id, cust_name, tel1, tel2, tel3 
FROM yourtable 
WHERE tel1 = 65432100 OR tel2 = 65432100 OR tel3 = 65432100

CodePudding user response:

I think maybe you can try this:

select *
from `your_table_name`
where TEL1 = '65432110' OR TEL2='65432110' OR TEL3='65432110';

  •  Tags:  
  • sql
  • Related