Query Logic SQL

2019-09-16 16:27发布

问题:

I have a table that contains this data.

main_id main_name other_name
   1      aa aa     bb bb 
   1      aa aa     cc cc
   2      bb bb     aa aa
   2      bb bb     cc cc
   3      cc cc     aa aa
   3      cc cc     bb bb

I have a field where user will search by name, when user searches for name the query should get data like main_name and other_name.

What I have tried so far is:

select where main_name like $data_entered or other_name like $data_entered

the result of this gives me

main_id main_name other_name
   1      aa aa     cc cc
   3      cc cc     aa aa
   3      cc cc     bb bb

I need to include:

main_id main_name other_name
   1      aa aa     bb bb

id did a query like this:

select where main_name like $data_entered or (main_name like $data_entered or 
other_name like $data_entered)

but still can't get:

main_id main_name other_name
   1      aa aa     bb bb

Any help would be appreciated

By the way, I'm coding php, with codeigniter framework.