I have a table of artist names such as Lady Gaga, Jason Mraz, Death Cab For Cutie, etc.
I want to be able to search for artists with no spaces. For instance, search the artists table where name is jasonmraz.
Is there any way to do this with mysql? Or should I create a new column in my table to hold these types of names?
SELECT REPLACE(your_col, " ", "") ...
or
SELECT your_col
WHERE REPLACE(your_col, " ", "") = "somestringsansspaces"
You can use the REPLACE function
SELECT REPLACE('Lady Gaga', ' ', '');
Will return LadyGaga
Using REPLACE(), as suggested, will work.
Bear in mind, however, that this is more work for your server, so if you have a large table of names (which I doubt you do, but never mind) you might want to consider adding a field of the names with no spaces.
Related
I have name column in my table containing list of business names.
The problem is there are some business names containing - , ' characters.
I am using this query to search the name
$names = Brand::where(name, 'LIKE', '%' . $request->search . '%')->get();
Example : a name from database daddy's barber shop
If I search this using this term daddys barber shop without ' character, it returns nothing.
How to ignore the ' character from database so it will match the daddy's barber shop ?
How to achieve this search?
You can whether go with #Rohit shah answer using whereRaw instead of where, but that is heavy processing especially when you'll have a lot of raws in your table, so everytime the replace charcters is processed.
What I would do if I had that case, is to create an other column in the tabale "name_alt" where i store the names without the unwanted charcters an make the searches on that column
SELECT
Column_name
FROM
Table_name
WHERE
REPLACE(REPLACE(REPLACE(name, '-',''), ',', ''), ''', '') LIKE '%search_string%'
This type of Query might serve your purpose.
Hope this helps you. Let me know in case of any query.
I have table named "city" with column named "city_name" with about 200 records.
I have created another colum named slugs where I want to copy all the records from "city_name" in the same row with spaces replaced with dash - and lowercase.
How can I achieve this via phpmyadmin.
Thanks
You should be able to do this via the following query:
UPDATE city SET slugs=LOWER(REPLACE(city_name, " ", "-"))
Breaking this down, we're using REPLACE to swap all instances of " " with "-" in the existing city_name column, and then passing the result of this to the LOWER function (to convert the data to lower case) before setting the slugs field with this value.
Depending on how "clean" your data is, you might also want to TRIM the data (to remove any leading or trailing spaces in the city_name field) before you apply the REPLACE as such:
UPDATE city SET slugs=LOWER(REPLACE(TRIM(city_name), " ", "-"))
Incidentally, if you've not used (My)SQL much I'd recommend a read of the String Functions manual page - the time you spend on this now will more than repay itself in the future.
Here is SQLFiddle
MYSql documentation for function LOWER(str) and REPLACE(str,from_str,to_str)
UPDATE city SET slugs = LOWER(REPLACE(city_name," ", "-"));
I have a table in a MySQL database which contains data like this;
ID text
1 Action Jackson
2 The impaler
3 The chubby conquistador
4 Cornholer
I want to display them in alphabetical order minus the leading "The ". This is what I've come up with which works.
SELECT ID, CASE LEFT(l.text, 4) WHEN "The " THEN CONCAT(RIGHT(l.text, LENGTH(l.text) - 4), ", The") ELSE l.text END AS "word"
FROM list l
This solution seems a little clunky, does anyone have a more elegant answer?
I think this is what you are looking for:
SELECT ID,
text
FROM list l
ORDER BY TRIM(LEADING 'The ' FROM text);
If you can at all, I would think of restructuring your data a bit.. Its hundreds of times better to rely on mysql indexes and proper sorting instead of doing it dynamically like this.
How about adding a field that drops the 'The ', and sort on that? You could make sure that this secondary field is always correct with a few triggers.
SELECT TRIM(LEADING 'The' FROM text) as word
FROM list
ORDER BY TRIM(LEADING 'The' FROM text)
My user table has a column "name" which contains information like this:
Joe Lee
Angela White
I want to search for either first name or last name efficiently. First name is easy, I can do
SELECT * FROM user WHERE name LIKE "ABC%"
But for last name, if I do
SELECT * FROM user WHERE name LIKE "%ABC"
That would be extremely slow.
So I am thinking about counting the characters of the input, for example, "ABC" has 3 characters, and if I can search only the last three characters in name column, that would be great. So I want something like
SELECT * FROM user WHERE substring(name, end-3, end) LIKE "ABC%"
Is there anything in MySQL that can do this?
Thanks so much!
PS. I cannot do fulltext because our search engine doesn't support that.
The reason that
WHERE name LIKE '%ith'
is a slow way to look for 'John Smith' by last name is the same reason that
WHERE Right(name, InStr(name, ' ' )) LIKE 'smi%'
or any other expression on the column is slow. It defeats the use of the index for quick lookup and leaves the MySQL server doing a full table scan or full index scan.
If you were using Oracle (that is, if you worked for a formerly wealthy employer) you could use function indexes. As it is you have to add some extra columns or some other helping data to accelerate your search.
Your smartest move is to split your first and last names into separate columns. Several other people have pointed out good reasons for doing that.
If you can't do that you could try creating an extra column which contains the name string reversed, and create an index on that column. That column will have, for example, 'John Smith' stored as 'htimS nhoJ'. Then you can search as follows.
WHERE nameReversed LIKE CONCAT(REVERSE('ith'),'%')
This search will use the index and be decently fast. I've had good success with it.
You're close. In MySQL you should be able to use InStr(str, substr) and Right(str, index) to do the following:
SELECT * FROM user WHERE Right(name, InStr(name, " ")) LIKE "ABC%"
InStr(name, " ") returns the index of the Space character (you may have to play with the " " syntax). This index is then used in the Right() function to search for only the last name (basically; problems arise when you have multiple names, multiple spaces etc). LIKE "ABC%" would then search for a last name starting with ABC.
You cannot use a fixed index as names that are more than 3 or less than 3 characters long would not return properly as you suggest.
However, as Zane said, it's a much better practise to use seperate fields.
If it is a MyIsam table, you may use Free text search to do the same.
You can use the REGEXP operator:
SELECT * FROM user WHERE name REGEXP "ABC$"
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
I have a table->column first-name with values:
Juan
Manuel l.
Richard Wit
I'm trying to do a query that return
Juan
Manul
Richard
I want to eliminate after a space.
Assuming you want SQL, try
SELECT MID(name + " a", 1, INSTR(name + " a", " ")-1) AS FirstName FROM myTable
In VBA it would be similar, since Mid() and InStr() work exactly the same as in Access-SQL
Really, though, it would be better to just store first and last name in separate fields so you don't have to do this sort of finagling in the SQL.
SELECT Left([First-Name], InStr([First-Name] & ' ', ' ') - 1) As CleanFirstName
FROM table
Keep in mind that running this will be inefficient since you will be executing VBA functions against every record in your query. Depending on how you are using this, you may want to have the query return the full field and do the processing after the fact.