I'm in a bit of trouble at the moment and have no idea how to fix this.
Some background info first:
I have a table with 71k records, with columns firstname and lastname. A client wants to be able to search with firstname and lastname combined.
I used this query and it works fine:
select *
from `subscribers`
where `subscribers`.`deleted_at` is null
and concat (firstname," ",lastname) = 'firstname lastname'
except for the following problem:
some records have a trailing whitespace behind their firstname. which makes the above query not work, unless I add a second space in between firstname and lastname.
I know I have to use RTRIM on my firstname, which I tried, but doesn't seem to work.
What am I doing wrong? How can I fix this?
I rather don't edit 71k records so they don't have a trailing space behind their names anymore...
Appreciate your help!
This should work (see SQL Fiddle demo):
select * from `subscribers`
where `subscribers`.`deleted_at` is null
and concat (trim(firstname)," ",trim(lastname)) LIKE 'firstname lastname';
Related
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
I am trying to INNER JOIN two tables on a text field, but I can't get it to work.
I've backtracked to see if I can identify where my query is falling down.
I've simplified my query to use only one table, to see if I can select data from it based on a text string.
I can't make it work.
I feel like an idiot.
This is so basic, why isn't it doing it?
select * from `my-table`
where myKey = "some-text-data-that-i-copied-from-the-data-in-the-table";
Returns no rows.
"some-text-data-that-i-copied-from-the-data-in-the-table" is in field myKey. I can see it. It's in the table!!!
[if it makes any difference I am most familiar with using an Access front-end and I am porting my "skills" to MySQL because MySQL is just better really.. this seems like the most run of the mill SELECT statement ever and yet it doesn't do anything!]
EDIT:
IMSoP does this help any...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
EDIT 2:
Diego Marinelli - thanks - this returns data
select * from mymathssowlink
where MyMathsResourceKey like '%KS2%';
EDIT 3:
This works...
select * from mymathssowlink
where MyMathsResourceKey like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
EDIT 4:
But this still doesn't...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
puzzled
Table:
CREATE TABLE `mymathssowlink` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ObjectiveID` varchar(13),
`MyMathsResourceKey` varchar(100),
PRIMARY KEY (`ID`),
UNIQUE KEY `idnew_table_UNIQUE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
HOW IT GOT FIXED:
okay very weird but I have fixed the simplified problem now! I remember from my Access days that trailing spaces are the bane of matches and it seems the MySQL is a bit fiddly in this regard to. There was no problem with the data as such but the field MyMathsResourceKey was the last bit of data on each line and so not terminated with a comma. Some lines didn't have any data in that column and so were terminated with a comma rather than the MyMathsResourceKey data. I fixed it by adding a third column to my csv into which i uniformly stuck the string "PLACEHOLDER". I matched this to a column called placeholder in the table receiving the data. Now that the active (2nd column) is always terminated with a comma all the data seems to work. I can't help thinking that this is a bit odd and that MySQL data imports really don't want to be behaving like this... now to try an make my JOIN work, which how this all started!
Thanks to all, especially Rahul for the help.
... and the JOIN Works now...
Going by all the edits in your post and comments; what I feel is, there is extra space present in your column value and so you are not getting the data (since it's not matching the exact string).
So either try like
select * from mymathssowlink
where MyMathsResourceKey
like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
(OR)
Use a REPLACE() function to remove all spaces in the field value
select * from mymathssowlink
where REPLACE(MyMathsResourceKey, ' ', '')
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can as well use TRIM() function if you can guarantee that the spaces are present only in left/right extreme of the string.
select * from mymathssowlink
where TRIM(MyMathsResourceKey)
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can try Select * from table; and if that works then try select * from table where key like '%some_text%'
It could not be a problem with the sintax but with the query itself.
in my table i have a feild user_ids. the user_ids feilds containeing the values like 12,45,78,95,21,78,98
what i need is i need an mysql query that search for a specific id(for ex:45) in the feild.
i used like operator but its not working as i expected. for ex: when i search for 5 its return tru, but the id 5 not in the list.
i would like to know is there any default function is available in mysql.
could you pls help me...
my query.
SELECT * FROM `FRIENDSLIST` WHERE `USERS_ID` LIKE '%'.$ID.'%';
NB: i dont know whether this question meets standard,pls dont do down vote. pls help me....
This also works:
select * from FRIENDSLIST where find_in_set( $ID, USERS_ID ) <> 0
Try
Where ',' + MyField + ',' Like '%,' + MySearchString + ',%'
The problem is that you're thinking of it as IDs, but it's just a string. So when you search for '5' in '12,45,78,95,21,78,98' it finds it in the 5 of the 45. If you surround with commas then it searches for ',45,' in ',12,45,78,95,21,78,98,' and finds it, but is you look for ',5,' it won't match, as desired.
you also need to add commas at the beginning and end to be able to math the first and last IDs.
As per your data simpler way is to search with comma as like ',45,'.
But better way is it split them based on comma and matching to it.
PROVINCE -- varchar(25)
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
Returns empty resultset when entries with Bakersfield as province exist
I am pretty unsure as to why. When I remove the WHERE it works.
BTW I am running this in phpmyadmin, where it lets one execute SQL statements
In phpmyadmin it shows ... beneath the equals sign. I am not sure why. That may be a hint.
If
SELECT * FROM listings WHERE PROVINCE like '%Bakersfield%'
works for you then you have spaces in your data. You can remove them with
update listings
set PROVINCE = trim(PROVINCE)
See TRIM()
After that you should check your code where you insert the data. Check why there are spaces inserted and fix it.
Change you equal = to LIKE operator
SELECT * FROM listings WHERE PROVINCE LIKE 'Bakersfield'
This should solve your issue. If you want to find places that includes your string simply surround it with wildcards %..%
SELECT * FROM listings WHERE PROVINCE LIKE '%Bakersfield%'
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
There is no point the abpve query doesnt work if the follwing case is not the problem.
1) If case is the prob then use upper or lower
SELECT * FROM listings WHERE upper(PROVINCE)=upper('Bakersfield');
2) If extra space is the problem use replace or Trim
SELECT * FROM listings WHERE replace(PROVINCE,' ','') ='Bakersfield'
There might be sum other seniario if not then use "Like " key word as suggested by others. :)
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.