SQL Query where a field value does not contain empty spaces - mysql

I am currently using the follow query:
SELECT *
FROM `wp_usermeta`
WHERE meta_key='avatar'
AND meta_key NOT LIKE '% '
ORDER BY RAND()
LIMIT 4
In that way, I want to try to get only field values, where no empty spaces re in the file name. Where is the error in my query? It still selects filenames with empty spaces in the filename.

Try
NOT LIKE '% %'
Your current wildcard match only catches trailing spaces.
Also, you're using meta_key twice. Should the column used in your LIKE clause be meta_value (or whatever it is in Wordpress).
This question is probably worth reading if you're concerned about performance - Which is faster — INSTR or LIKE?

Related

Use Astrisk mark in sql query with like operator

I am having a issue with sql query.
This is the query I used to search the result
SELECT * FROM wp_wp_campaigns
WHERE `campaign_status`='1'
AND `campaign_type`='o'
AND `campaign_name_decoded` LIKE '%Deep%'
OR `campaign_name_decoded` LIKE '%Deep'
OR `campaign_name_decoded` LIKE 'Deep%'
OR `campaign_name_decoded` LIKE 'Deep_'
OR `campaign_name_decoded` LIKE '_Deep'
OR `campaign_name_decoded` LIKE '_Deep_'
ORDER BY id desc
LIMIT 10 OFFSET 0
It works perfect when used with only text.
But does not return any value when input value is like Deep*
Any help in this is highly appriciated.
This does not directly answers your question. But if you want the filtering on campaign_status and campaign_type and then on the campaign_name_decoded, then this is sufficient:
WHERE campaign_status = '1' AND
campaign_type = 'o' AND
campaign_name_decoded` LIKE '%Deep%'
The % wildcard matches zero or more characters, so is should be doing what you want.
That said, if some campaign names are not matching, that is because you have invalid (unexpected?) characters in either the data column or the comparison.

MySQL - need to find records without a period in them

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 '[\\.]';

Parameterized string and wildcards in MySQL

I'm trying to build a MySQL query for a database search operation, where a user can specify a text string to match against a particular column. I figured that using the LIKE operator and surrounding the user input with % signs, to act as wildcards, would be best practice. I want the wildcards to be there on both the start and end so the user does not have to enter the whole string. Furthermore, I'd like to parameterize the query to avoid injection and whatnot. This leaves me with a query that looks something like this:
SELECT * FROM `sometable`
WHERE `name` LIKE ?
ORDER BY `id` ASC
LIMIT 1,10
(Note that the name column is a VARCHAR(50) with collation utf8_general_ci.)
The parameter from the WHERE clause is added like so:
Using cmd As New OdbcCommand()
cmd.Parameters.AddWithValue("name", "%" & strUserInput & "%")
...
However, what I now ended up with appears to be MySQL actually matching the name column against the concatenated string, treating the %'s as literals and not as wildcards as I had intended.
I also tried LIKE CONCAT('%', ?, '%'), but this doesn't work either.
How would I glue a wildcard character to the start and end of a parameterized string? Or is there a much better way of doing this?
Your SqlParameter name is #name not name : cmd.Parameters.AddWithValue("#name", string.Format("%{0}%", strUserInput);
And your sql should be:
SELECT * FROM `sometable`
WHERE `name` LIKE #name
ORDER BY `id` ASC
LIMIT 1,10
Apparently I oversaw something really stupid. The LIMIT clause was the problem - the database I was testing with was supposed to return only one or two rows per query, but because I specified 1,10 it skips the first result. I did not know LIMIT is zero-based. Oops.
Learning oppurtunity, I guess.

Mysql SELECT all rows where char exists in value but not the last one

I need a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO

how to search for an id from a group of id using mysql?

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.