I have a typeahead select box for finding users in a database. Frequently there are meny users with the same first name & different last names.
A person searching for a user would generally type in: "John doe".
The problem with this is that the typeahead will start getting all the John users but then return no results when the space is typed in.
"WHY" this is happening is obvious, user first names are trimmed & anything with a space will not match ~ let alone with another character following that, first & last names are stored in separate columns.
Here is the query as it stands:
SELECT Entities.* FROM `Entities`
WHERE `Entities`.`first_name` LIKE '%john doe%'
OR `Entities`.`last_name` LIKE '%john doe %'
How can I re-write this query so that the results would be:
all users whose first name is john
ignore any amount of spaces
AND where last name is doe
when a user types in"john doe" in the search control
(Keeping in mind that the search control is ONE text type field)
If you want the first name like john and the last name like doe, then try something like:
where concat(Entities`.`first_name`, ' ', `Entities`.`last_name`) like '%john% doe%'
If you want do do this based on a search string, then something like:
where concat(`Entities`.`first_name`, ' ', `Entities`.`last_name`) like
concat('%', replace('john doe', ' ', '%'), '%')
Or, use full text search.
Related
I tried to write it like this.
WHERE LastName LIKE 'M%'
but did not complete all
You'll want something like:
select LastName from people where LastName like "M%ae%"
Each % is a wild card for an unknown number of unknown letters.
(Replace people with the name of the relevant table, and the first occurrence of LastName with the information you want from the table)
Is it possible in MySQL to do a 'like' in the statement that matches any part of a string.
It's hard to explain but I can give an example.
I am concatenating two columns together to display a full name (e.g John Barry Smith) the first name is 'John Barry' the last name is 'Smith'
I am making a filter and I would like it where I can type in 'John Smith' , which will then be passed through as the 'like' (or something similar) in the where clause and it will bring up the user rather than having to type 'John Barry' as not all of the users in the table have a middle.
Hopefully i've been clear enough in my question.
You could do something like this:
WHERE CONCAT(first_name,' ', last_name) LIKE ('john%smith')
To list all "John sth sth Smith"s
Or even LIKE ('%john%smith%') to list al "sth sth John sth sth Smith sth"s (just replace all spaces with %'s)
An other option would be to split your search on spaces and do a LIKE for each word to list al Johns, and all Smiths, but this would be irrelevant I think.
Let me start off be saying I am new to trasactsql and query so please be kind.
I have a DB with about 1700 records for my product file, and one field (name) has the product code, a space and then the name of my product. I have another field (pcode) with just the product code that has numbers and possibly a letter or two that matches the beginning of the "name" field. I need to remove all characters in front of the space in 'name" including the space. Is there a way to remove the characters if they match what is in "pcode"?
Here is an example.
What I have;
pcode | name
1234 | 1234 widget
What I want;
pcode | name
1234 | widget
There are some names with spaces in them. ie. "Left Handle Button side" or "Control "Lever"
Thanks in advance.
Michael
In a select, you can do:
select pcode, substring_index(name, ' ', -1)
. . .
In an update:
update table t
set name = substring_index(name, ' ', -1)
where name like '% %';
If you want to change the data in place (using an update), check the logic (or copy the table) before doing the update. This might not do what you expect, for instance, if the name itself has a space in it.
Try to export it to MS Excel for example and do it manually. Next time learn about 12 gold rules when creating SQL database.
http://en.m.wikipedia.org/wiki/Codd%27s_12_rules
I'm trying to create a SQL query which will supply values for auto completion for a text field. Everything is working however I can't seem to create an SQL query which is exact enough for the purposes I want. I am using MySQL.
If there is a space (or multiple spaces) in the search term, I only want the query to do a LIKE comparison on the part of the string after the last space.
For example, say I have two possible values in the database:
Bolt
Bolts Large
Currently if the user types 'Bolt' then a space, both values above are returned using this query -
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM%'
What I want is that if the user types 'Bolt' then a space, then only Bolt is returned from the database.
Effectively meaning that only the last part of the search term after the space is compared using LIKE, the results should match exactly up until the last space.
I've also tried:
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM[a-z]%'
But that actually returns no results using the above scenario.
Is what I'm after possible? I've also tried to explore using Full Text Search but have had no look with that. I believe full text search is enabled on the name field, however I have limited experience with this. The query below didn't work.
SELECT name FROM items WHERE MATCH(name) AGAINST('SEARCH_TERM')
Any advice or points would be very appreciated.
The query
SELECT name FROM items WHERE name LIKE 'Bolt %'
doesn't return any record, because both 'Bolt' and 'Bolts Large' don't match 'Bolt %'.
SELECT name FROM items WHERE name LIKE 'Bolt%'
returns both records, because both 'Bolt' and 'Bolts Large' match 'Bolt%'.
To look for 'Bolt' and not 'Bolts', you must add a space to both your search string and the column string:
SELECT name FROM items WHERE concat(name, ' ') LIKE 'Bolt %'
returns 'Bolt' but not 'Bolts Large'.
SELECT name FROM items WHERE REPLACE(name, ' ', '') LIKE 'SEARCH_TERM%'
You could also use CONCAT and TRIM, or just trim
SELECT name FROM items WHERE name LIKE TRIM('SEARCH_TERM')
or your choice
SELECT name FROM items WHERE name LIKE CONCAT(TRIM('SEARCH_TERM'), '%')
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'))
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'), '%')
i need to select only the first name (that is the first word) of the user.
For example, Andy Jones, only select Andy
Any idea?
thanks
Take a look at substring_index.
select substring_index(field, " ", 1) ....
If I understand, your FirstName and LastName are in one column. You can achieve this using user defined functions.
Example:
SELECT SPLIT_STR(name, ' ', 1) as firstname FROM users;
Read following post for more options:
Split value from one field to two
For something like this you would normally put the first and last name in their own columns. If you cannot, then you can get the index of the first space, then substring(0,index of the first space). I would really recommend though that you split that into two columns, if you can.