Mysql search with PHP
I'm using PHP to search a Mysql DB, i'm echoing the query so i can see whats going through, its coming out as this:
select * from `parts` where 'category' like 'at'
However nothing is getting returned. I know there is definitely a column called "category" and a field called "at". If i do %at% it 开发者_如何学JAVAjust returns every result!
Essentially i want to get all the records where at is a value in the category column
select * from
parts
where 'category' like 'at'
This doesn't make any sense - the quotes around category declare it to be a literal value rather than an attribute name.
I know there is definitely a column called "category" and a field called "at".
This is getting confusing. The term 'column' is often in place of 'attribute' (the latter is the correct way to describe the data structure within a relational database). As for 'field' - do you mean an instance of an attribute?
I think you mean your code to do this:
select * from parts where category like '%at%'
(the backticks
are optional around database entities - i.e. non-literal values - unless they include spaces). Note that using te like operator without wildcards is messey so if you're looking for the values matching 'at' and not 'cat' or 'attach'....
select * from parts where category='at'
It's not clear from your question which should be literals and which should be field names. Use left apostrophes for field names. So, if you want all records where category field contains literal "at", do:
Change your quotes:
select * from `parts` where `category` like '%at%'
Not sure what do you mean by field called "at" but if it a row value Try this
select * from `parts` where category='at';
You cannot really compare two fields using like in this way. The second argument to Like is an pattern and interpreted as a regular expression. You can try this:
select * from parts where category like concat('%', at, '%')
It will take the value from the at field, add the % signs then compare the fields.
精彩评论