Replace function in mysql not working - mysql

I have a table like this;
Lastname
MORALES
THOMPSON
SMITH
but I want to use the replace function to change all the lastname to another character like this:
I want 'MORALES' to be replace with 'TEYE'
I tried this syntax;
select lastname, REPLACE(lastname, 'M', 'TEYE')
from customers;
but this is what i'm getting;
'MORALES TEYEORALES'
Instead of
'MORALES TEYE'
please I need help.
Thanks

You told it only to replace M, not the whole name, that's what it did. If you want to replace the whole name, write:
SELECT lastname, REPLACE(lastname, 'MORALES', 'TEYE')
FROM customers
REPLACE() replaces substrings, so if there's a lastname = AMORALES, the result will be ATEYES. If you only want to replace it when it's the entire name, you can use:
SELECT lastname, IF(lastname = 'MORALES', 'TEYE', lastname)
FROM customers

not an expert in mySQL; but, it looks like you are getting what you asked for, the last name folowed by the last name where the "M" was replaced with "TEYE".
have you tried appending the string you want added?
select lastname + " TEYE" (or some variation to concatenate in mySQL syntax.
if the last name only needs to be update if it currently equals "MORALES", you can most likely add a where (row selection) clause (something like where lastName = "morales") to filter the rows being updated

Related

How to find names that contain a single ā€œeā€ in MySQL

My table name is student and column name is FullName.
Can anyone help with this question? I have tried:
select FullName from student where fullName like "e"
But this is returning 0 rows.
If you want students that contain at leas one 'e', then:
select fullname
from student
where fullname like '%e%'
Note the use of the wildcard character (%) around the e, which searches for the character anywhere in the string.
But if you really mean students that contain a single e (not more, not less), then you need to filter out names that contain more than one. For this, you can do:
select fullname
from student
where fullname like '%e%' and fullname not like '%e%e%'
You could also use replace() and char_length():
select fullname
from student
where char_length(replace(fullname, 'e', '')) = char_length(fullname) - 1
You can use a regular expression:
select fullname
from student
where fullname regexp '^[^e]*e[^e]*$'

BETWEEN IN in SQL

I have my_own table with columns - PersonID, FirstName, LastName, City, age and WorkStatus. Write BETWEEN IN syntax with WHERE condition, it doesn't return any rows but doesn't give any error.
SELECT PersonID,FirstName,LastName,City,age,WorkStatus
FROM sakila.my_own
WHERE (FirstName BETWEEN 'Omer' AND 'Amrah')
AND NOT WorkStatus IN ('Unemployee');
Why does it do that? If any one has any answer, please share with me.
Thanks in advance for answers.
you query:
SELECT PersonID,FirstName,LastName,City,age,WorkStatus
FROM sakila.my_own
WHERE (FirstName BETWEEN 'Omer' AND 'Amrah')
is syntactically correct but not semantically correct. More specifically the clause: FirstName BETWEEN 'Omer' AND 'Amrah' will always result in an empty set, because 'Omer' > 'Amrah', string wise. Try:
FirstName BETWEEN 'Amrah' AND 'Omer'
You should change that BETWEEN condition to using LIKE operator like
WHERE FirstName LIKE '%Omer%' Or FirstName LIKE '%Amrah%'
AND WorkStatus <> 'Unemployee';
You are comparing string using BETWEEN.
So obviously it will did not compare it as numeric or date field.
And one more thing
AND NOT WorkStatus IN ('Unemployee');
Instead of this you have to put
AND WorkStatus NOT IN ('Unemployee');

search box -> I need to concat first name and last name from one table and join it to a 2nd and return item from 2nd table

So the search would be like 'Ozzie Smith'
First table has (username, fname, lname) = osmith, ozzie, smith
Second table has (username, team) = osmith, cardinals
I need to concat the first and last name columns from the first table join it by username on the second table and return the team name.
I have been trying and trying...brain melted. I need your help!
Thanks!
Since it's MySQL, you need to use CONCAT, not the + sign. I also added a LOWER() function to avoid capital letter mismatch problem :
select team
from table1
join table2 on table2.username = table1.username
where lower(concat(fname,' ',lname)) like lower('%Ozzie Smith%')
You're probably doing something like
WHERE fname LIKE '%Ozzie Smith%'
which will not work. fname will contain only Ozzie, and will never match against Ozzie Smith. It would also not match if the user enters Smith, Ozzie, or any other dual-name variant. You'll have to preprocess the search terms and do something like
WHERE (fname LIKE '%Ozzie%') or (fname LIKE '%Smith%')
OR (lname LIKE '%ozzie%') or (lname LIKE %Smith%')
This will get VERY painful as the number of search terms and fields being search goes up. You'd be better off using a FULLTEXT index, where it'll be a simple matter of
WHERE MATCH(fname, lname) AGAINST ('Ozzie Smith')
Why doesn't this work?
select
lname + ', ' + fname as playerName
, team as teamName
from table1
join table2 on table2.username = table1.username
Update:
where (fname + ' ' + lname) like '%Ozzie Smith%'

Sql query returns only one row (LIKE)

SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.

mysql SELECT * FROM table WHERE column+space+column LIKE '%query%'

For a search function, I need to be able to get the mysql function to show up results by combining two columns.
To be more precise: I have a table in the database called "members" in that there is a column called "firstname" and one called "lastname" which I need to use for this. When someone types in a whole name, eg. Jane Doe, I want the query to look whether combining firstname + a space(&nbsp?) + lastname brings forth any results.
My code so far is this:
$poster = mysql_query("SELECT id FROM members WHERE (firstname'&nbsp'lastname) LIKE '%$search%'") or die(mysql_error());
$poster = mysql_fetch_object($poster);
I know that's probably wrong, it's the most recent I've tried after trying with brackets around the firstname + lastname bits, etc, etc... But yes, any help would be greatly appreciated.
Use the CONCAT function:
SELECT id FROM members
WHERE CONCAT(firstname, ' ', lastname) LIKE ?
Try CONCAT
SELECT id FROM members WHERE concat(firstname, ' ', lastname) LIKE '%$search%'
SELECT id FROM members WHERE (firstname + ' ' + lastname) LIKE '%$search%'
I believe what you are looking for is:
$name_var = 'John Doe';
// Example 1 - using concatenation
$sql = "SELECT id FROM members WHERE CONCAT(firstname, ' ', lastname) = '$name_var'";
The above statement will search for everything where the first name is John and the last name is Doe
This is rather ineficcient as it will have to evaluate the CONCAT everytime in mysql I believe
I would reccomend validating in PHP that the string is two words as you expect e.g.
$name_var = 'John Doe';
// this will split the string based on spaces
$names = explode(' ', $name_var);
$first_name = $names[0];
$last_name = $names[1];
// Example 2 - searching each field
$sql = "SELECT id FROM members WHERE firstname = '$first_name' AND lastname = '$last_name'";
The above statement will still search for everything where the first name is John and the last name is Doe
In the above statment you are actually just searching based on the exact values so it is much more efficient. If this query is going to be ran regularly, you should also add indexes to the firstname and lastname fields in your mysql table as it will greatly increase the speed!
hope this helps!
Tom