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
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.
Related
I'm using below sql query for search values from db, but when I search 'study' it will returns the values 'caese-study', 'get-study-materials' as well.
How can I use a query to search exact contains withing string column?
$names = 'study';
and the names column has values like comma separated,
Ex: 'study, abc, new' so I need to search within that too
SELECT * FROM datatitle WHERE names LIKE '%$names %' ;
SELECT * FROM datatitle WHERE names regexp '(^|[[:space:]])$names([[:space:]]|$)';
I try with above two queries but didnt work as expect, pls advice?
You should not be storing comma-separated values in a column. You should be using a junction/association table. You should fix the data model, if you can.
However, sometimes we cannot control other people's really bad decisions. MySQL has find_in_set():
SELECT dt.*
FROM datatitle dt
WHERE find_in_set(?, names) > 0;
Note that I have replaced the constant $names with a parameter. You should learn to use parameters to pass values into queries.
I would like to write an sql statement for search. Here is sample database structure.
For eg, I want to display all records with topic '13'. How can i write sql query for searching 13 from the above structure? Any suggestions?
Can i able to use WHERE Topic LIKE '%13%'? Anything wrong with this?
Try this one:
SELECT * FROM `TABLE_NAME` WHERE `Topic` LIKE "%13%";
It's better and faster to save it in a third table of many-to-many relationship.
If you want to save as per your example (single table), try to save data as eg ",10,13,15,"
always have coma before and after, thus the following sql will exclude 213 and 132 etc
select * from table_name where Topic like '%,13,%'
select * from table where find_in_set("13",topic);
or if topic is not used as a set, you could do ...
select * from table where concat(",",topic) like "%,13,%";
The 2nd isn't real elegant but I've had to do that a couple times.
Because the data isn't really normalized, I used concat to add a comma to the topic field so I could make sure the "like" comparison would pass with a comma before and after the value. I suppose we would also have to remove any unwanted spaces as well for this example, so ultimately it would end up like:
select * from TABLE where concat(",",replace(topic," ","")) like "%,13,%";
Ultimately, we have to know what to expect in the topic column to come up with a query that would always work. In the past, I've had situations where I would add values to a string field (i.e. topic) with a delimiter before and after each value like:
(1)(2)(3)(14)(15)(255)(283)
If you did something like this for the topic field, the query is simple ...
select * from table where topic like "%(13)%";
I have a search form using a single text input with PHP, MySQLi.
Users search for names of people. In the MySQL table is a column 'name' and also a column 'nameNoSpaces' - this value is used when matching a user's search.
On input, the users query is stripped of all whitespace so that it also has no spaces. then I use the LIKE operator to compare the user's input (with no spaces) to the column 'nameNoSpaces'.
SELECT * FROM table WHERE nameNoSpaces LIKE '%query%'
If a value in 'nameNoSpaces' contains any part of the query string, it will be matched.
E.G. query 'hello you' will be matched to the value 'goodhelloyou'.
But not : query 'you hello'
I want to improve the search capabilities so that 'you hello' will return the row containing 'goodhelloyou'.
I have achieved this with PHP (foreach word do 'LIKE '%word%' and return all names that match every word).
BUT I was wondering if there is simply a SQL operator I can use to achieve this??
Thanks
SELECT * FROM table WHERE nameNoSpaces LIKE '%hello%you%'
Figured it out...should of seen it before
SELECT * FROM table WHERE name LIKE '%word1%' AND name LIKE '%word2%'....'%wordn%'
put all words into an array and use a foreach loop to create the query
I want to query a table as follows:
I have a field called "category" and my input match contains N separate words. I want the query to match all rows that contain all N words, but in any order.
For example if the field category contains "hello good morning world", my input query can contain "hello morning" or "good" or "world hello" and all are matches to the query.
How do I formulate such an SQL expression?
Also it would be good if the query can be made case insensitive.
If you are using MySQL you can use the boolean fulltext search feature to achieve this. You can put a + in front of each term and then only results with all the terms, in any order, will be returned. You will need to make sure the column containing the category field has a fulltext index specified on it for this to work. Other database engines probably have similar features. So for example you might do something like the following assuming there were a fulltext index over the category column...
SELECT * FROM myTable WHERE MATCH (category) AGAINST ('+term1 +term2 +term3' IN BOOLEAN MODE);
I would avoid using the "LIKE" operator as others have suggested you would have to worry about the headache of mixed upper/lower case and if you have a large database using a % in the front of a LIKE search term is going to cause a full table scan instead of using an index which is horrible for performance.
I'm not writing the loop that will build this query for you. This will get the job done, but it will be pretty inefficient.
SELECT * FROM table
WHERE (
TOUPPER(category) LIKE '*HELLO*' AND
TOUPPER(category) LIKE '*GOOD*' AND
TOUPPER(category) LIKE '*MORNING*' AND
TOUPPER(category) LIKE '*WORLD*'
);
You could also research using REGEXes with SQL.
I am having a table with a column that has few ids that were put into database with multi select. Column for example contains: 1,4,5,7,9. Is it possible to check if this column contains for example number 5 or not in it through MySQL query ?.
I need to select all the people that have number 5 or some other listed in that field and print them through php.
http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set
SELECT ...
WHERE FIND_IN_SET(5, list_column)
But understand that this search is bound to be very slow. It cannot use an index, and it will cause a full table-scan (reading every row in the table). As the table grows, the query will become unusably slow.
Please read my answer to Is storing a delimited list in a database column really that bad?
You can use #MikeChristensen's answer to be more standard. Another trick with standard SQL is this:
select * from TableName
where ',' || ids || ',' LIKE '%,5,%'
(in standard SQL, || is the string concatenation operator, but in MySQL, you have to SET SQL_MODE=PIPES_AS_CONCAT or SET SQL_MODE=ANSI to get that behavior.)
Another MySQL-specific solution is to use a special word-boundary regular expression, which will match either the comma punctuation or beginning/end of string:
select * from TableName
where ids RLIKE '[[:<:]]5[[:>:]]'
None of these solutions scale well; they all cause table-scans. Sorry I understand you cannot change the database design, but if your project next requires to make the query faster, you can tell them it's not possible without redesigning the table.
Perhaps:
select * from TableName
where ids = '5' -- only 5
or ids like '5,%' -- begins with 5
or ids like '%,5' -- ends with 5
or ids like '%,5,%' -- 5 in the middle somewhere
It probably won't be very fast on large amounts of data. I'd suggest normalizing these multi-selection values into a new table, where each selection is a single row with a link to TableName.
select * from your_table where concat(',',target_column,',') like '%,5,%'
you can write the sql query like this, for example you are looking for the number 5
select * from your_table_name where ids='5'
if you want to check the result with php just tell me i will write it for you :)