reksss Posted April 19, 2011 Share Posted April 19, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/ Share on other sites More sharing options...
amolv Posted April 19, 2011 Share Posted April 19, 2011 like will be more helpful friend... Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1203377 Share on other sites More sharing options...
Muddy_Funster Posted April 19, 2011 Share Posted April 19, 2011 perhaps something like: SELECT color FROM table_colors WHERE color = 'green' OR color='red' OR color = 'blue' Although that's probably too simple, but I don't really get the question.... Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1203379 Share on other sites More sharing options...
kickstart Posted April 19, 2011 Share Posted April 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1203383 Share on other sites More sharing options...
reksss Posted April 20, 2011 Author Share Posted April 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1203804 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 Could you post up your table structure please? This looks like it's headed at getting messy and we may need to re-evaluate your database before it gets much further. Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1203870 Share on other sites More sharing options...
kickstart Posted April 20, 2011 Share Posted April 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234133-how-to-comparing-two-or-more-string-in-php/#findComment-1204121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.