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. :)
Related
I have value in my db
like
1,12,13,25,44,414,2114
I have to find exact 14 from the db.
but it also return the value 414 and 2114
but i want exact 14.
How can i achieve this through sql query Please help
i have tried this but didn't worked.
Select * from tb_name where columnNAme like '%value%'
If you want search exactly 14, then you need search %,14,%, but 14 may appears at start or at end of string, so you need add commas to column also.
Well, you can use:
select * from tb_name where concat(',',columnNAme ,',') like '%,14,%'
Side note, comma separated values in one column is bad database design
Select * from tb_name where columnNAme like 'value'
% - The percent sign represents zero, one, or multiple characters
SELET * FROM database WHERE column LIKE 14 should work fine, just double checked it on my database.
If this doesn't work can you show your query please?
Above answer is correct, I didn't see your query before I posted this.
I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';
I'm trying to find all entries that contain a backslash anywhere, like so:
SELECT * FROM animals WHERE bodyType LIKE '%\\%'
I also tried:
SELECT * FROM animals WHERE bodyType LIKE '%\\\\%'
and
SELECT * FROM animals WHERE bodyType LIKE '%\\\\\\\\%'
Neither worked. Anyone know how to do this?
I am running the commands in MySQL Quick Admin v1.5.4
This question has not answer.
You try to match searching of content with backslash on names of columns.
Your (a bit general) query
SELECT * FROM tableName WHERE columnName LIKE '%\\%'
will give you any result if content of any column will contain backslash. In this case your query is correct. But you cannot match name of any column.
I looked into some books I have and all they say the same: this will select all records written in column of chosen name that are containing backslash. But columns have to be chosen exactly (they cannot be selected by name with using of SQL query).
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.