2

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 just returns every result!

Essentially i want to get all the records where at is a value in the category column

7
  • What type is the field? What collation is the table? Commented Dec 8, 2010 at 13:44
  • Please tell us more about the cateogry field and the values it contains. eg is it fixed at two characters or does it allow longer strings? Is it case sensitive? Please show us some of the other values that are selected when you do "%at%"; is it really all values or could it be that you just have lots of categories that contain the letters 'at' somewhere in them. Have you tried "at%"? Commented Dec 8, 2010 at 13:49
  • 1
    he is trying to compare two fields. Commented Dec 8, 2010 at 13:53
  • weirdly i tried at% and i get a 406 error from my webhost!? Commented Dec 8, 2010 at 13:54
  • 1
    lol, remove the quotes around 'category'. Of course 'category' is like '%at%', it contains 'at' :) Commented Dec 8, 2010 at 13:54

4 Answers 4

2

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'
Sign up to request clarification or add additional context in comments.

Comments

1

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%'

Comments

1

Not sure what do you mean by field called "at" but if it a row value Try this

select * from `parts` where category='at';

1 Comment

no, he says " i want to get all the records where at is a value in the category column"
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.