Jump to content

How to comparing two or more string in PHP?


reksss

Recommended Posts

 

Hello Friends....

 

I want to compare two or more strings by using Mysql select statement.

Suppose for example i need to check "red","green","blue" values from the Mysql column 'color' and  need to display all values

I tried

 

select * from table_colors where color IN ('green','blue','red');

 

I think IN operator will work only for integer.

Do any one know which operator may i use for getting the ouput....

 

 

Link to comment
Share on other sites

Hi

 

select * from table_colors where color IN ('green','blue','red');

 

Above will work fine to bring back any row from table_colors where the color is green, blue or red. IN works fine for non integers, as it is checking that the column color has a value of green, blue or red.

 

However if you want to check for a column which partly contains red (ie, say you wanted to match red, dark red and light red) then you would need to use like and check each colour individually.

 

All the best

 

Keith

Link to comment
Share on other sites

 

Dear Friends....

Thanks for your reply.

 

Actually i will get input from client and i need to search in mysql and display the result as HTML table.

 

in db the column color has values like listed below

 

red|blue|green

blue

yellow|orange

blue|yellow

red

yellow

light green

dark green

light yellow

violet

 

If the user will key input like 're,blue' (or) 'red,blue' i need to listed the output like

red|blue|green

blue

blue|yellow

red

 

Sometimes the user may give more input like 10 or 15 colors. If so, how will get all the input.

 

Actually im doing same like for gene_id column. Their the column datatype is integer and the user will give the correct number.

So  'IN operator is working well'.

 

But for colors if the user will key input like "yel" for yellow, i need to search for yellow and need to print.

I find '%' sign is not working in the 'IN' operator.

 

For matching one data, 'LIKE' is ok. But if search for 10 or 15 datas, then how should i do? I must need to write query like this

 

select * from table_color where (color LIKE 'red') AND (color LIKE 'yell') AND (color LIKE 'brown') AND (color LIKE 'bro') AND (color LIKE 'yellow') AND (color LIKE 'n')

 

Is there any other way to do this?

Some times the user may input like 20 to 30 data's means how will we do?

 

Link to comment
Share on other sites

For matching one data, 'LIKE' is ok. But if search for 10 or 15 datas, then how should i do? I must need to write query like this

 

select * from table_color where (color LIKE 'red') AND (color LIKE 'yell') AND (color LIKE 'brown') AND (color LIKE 'bro') AND (color LIKE 'yellow') AND (color LIKE 'n')

 

Is there any other way to do this?

Some times the user may input like 20 to 30 data's means how will we do?

 

Unfortunately not, although you would need to use "%" in each LIKE statement.

 

If the list of colours is in an array then you can automate the creation of the SQL.

 

However I agree with Muddy_Funster that you might be better off redesigning the tables. Possibly with a tables of colours with a row per colour for each row on the current table.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.