Use Astrisk mark in sql query with like operator - mysql

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.

Related

mysql query not like several text matches

I am trying to do a simple query that has a where clause stating there is no match for 2 items:
where l.country not like \"%USA%\" or \"%CA%\" ORDER BY l.state
I also tried:
where l.country not like \"%USA%\" or l.country not like \"%CA%\" ORDER BY l.state
also tried:
where l.country not like (\"%USA%\", \"%CA%\") ORDER BY l.state
is there a way to use "not like" with more than one match?
This is your original condition:
where l.country not like "%USA%" or "%CA%" ORDER BY l.state
I assume you intend this to mean "the country is neither the USA nor CA."
If so, you would write it this way:
where l.country not like '%USA%' and l.country not like '%CA%' ORDER BY l.state
But there's no syntax in SQL for NOT LIKE 'X' OR 'Y'. The LIKE predicate has a left operand and a single right operand, no more.
The expression you wrote is a valid expression, but doesn't do what you think it does. It's like as if you had written this:
where (l.country not like "%USA%") or ("%CA%") ORDER BY l.state
That is, two terms, separated by OR, the first is a LIKE comparison, and the second is just a single string literal on its own. That's a valid term in an expression, but it doesn't do anything useful. It's like writing:
x = 6 * 8 + 0
What effect does the zero have in that expression? None.
Update: I was mistaken, I overlooked one effect of the query as you wrote it. You should know that in a boolean expression if you OR two terms, it doesn't matter what the first term is if the second term is always TRUE.
WHERE (some expression) OR (TRUE)
This is always true.
The literal string '%CA%' counts as true, because it's not an empty string or a NULL. So in your original query, the WHERE clause is always true no matter what the country is.
You could use REGEXP with an alternation:
SELECT *
FROM yourTable
WHERE country NOT REGEXP '"%USA%"|"%CA%"'
Notes:
You don't need to escape double quotes which appear inside of string literals in single quotes. Your original query would not run, I think, because you need to compare a column using LIKE against either another column or a string literal, normally in single quotes.
REGEXP is not case sensitive, so we could have used usa and ca for the same result, though this does not appear to matter in your case.

THEN statement in a SQL query

I'm trying to alter a Wordpress search query so that it returns broader results.
The scenario:
There is a post entry titles "Carousel Watch" in the database. When I search either (or both) of these words, I get the post returned, which is what I'd expect.
If, however, I search for "Carousel Watch Gift", I do not get any results.
The SQL query for this search is the following:
SELECT SQL_CALC_FOUND_ROWS sosen_posts.ID FROM sosen_posts WHERE 1=1
AND (((sosen_posts.post_title LIKE '%carousel%') OR (sosen_posts.post_content LIKE '%carousel%'))
AND ((sosen_posts.post_title LIKE '%watch%') OR (sosen_posts.post_content LIKE '%watch%'))
AND ((sosen_posts.post_title LIKE '%gift%') OR (sosen_posts.post_content LIKE '%gift%')))
AND (sosen_posts.post_password = '') AND sosen_posts.post_type = 'wp_aff_products' AND (sosen_posts.post_status = 'publish')
ORDER BY
(CASE WHEN sosen_posts.post_title LIKE '%carousel watch gift%'
THEN 1
WHEN sosen_posts.post_title LIKE '%carousel%' AND sosen_posts.post_title LIKE '%watch%' AND sosen_posts.post_title LIKE '%gift%'
THEN 2
WHEN sosen_posts.post_title LIKE '%carousel%' OR sosen_posts.post_title LIKE '%watch%' OR sosen_posts.post_title LIKE '%gift%'
THEN 3
WHEN sosen_posts.post_content LIKE '%carousel watch gift%'
THEN 4 ELSE 5 END), sosen_posts.post_date DESC LIMIT 0, 3
My questions are:
What part of the query prevents returning results ("Carosuel Watch")?
How I should alter it to get the result returned?
And finally, what does the THEN statement do in this query? Does it set the value to compare in the WHERE statement?
The THEN keyword is used as part of a CASE clause. A CASE clause looks like this:
CASE WHEN expression THEN value WHEN otherexpression THEN othervalue ... END
The result of the expression is a single value (sometimes you see people try to use a CASE clause to determine what code will execute, and this won't work).
In this query, the value from the CASE claues is used to determine sort order for posts, and has not bearing on which records are included or not included in the results.
To find out which records are included in the results, we need to dissect the WHERE clause. I re-formatted the existing clause for easier reading below:
WHERE 1=1 AND (
(sosen_posts.post_title LIKE '%carousel%' OR sosen_posts.post_content LIKE '%carousel%')
AND (sosen_posts.post_title LIKE '%watch%' OR sosen_posts.post_content LIKE '%watch%')
AND (sosen_posts.post_title LIKE '%gift%' OR sosen_posts.post_content LIKE '%gift%')
) AND sosen_posts.post_password = '' AND sosen_posts.post_type = 'wp_aff_products' AND sosen_posts.post_status = 'publish'
First, the 1=1 part. This is common for autogenerated code. The generator can put a WHERE 1=1 at the beginning of the WHERE clause whether or not there are any conditions, and the query will still be valid. Then, for each condition, it can always use the form AND condition, without worrying about the prior state of the WHERE clause.
Moving into the next section, we see it checks each keyword individually against both the title and text. These checks are connected via AND operators. This means that if your post does not have the word gift in the title or body somewhere, it cannnot appear in the results.
Finally, this code is incredibly inefficient. You never want to see a LIKE operator with a leading wildcard (%), because it pretty much guarantees that you can't use any indexes to satisfy that condition. I'm more of a Sql Server guy, but in Sql Server -land what you want to do instead is create a special kind of index, called a full text index, and write the query using a special CONTAINS() clause. Failing that, you use a 3rd-party search library such as Lucene. You never want to use LIKE queries for searches like this. I'm not sure what the MySql equivalents to these alternatives are, but what you have here is not the way you want to be doing this.
The LIKE clauses require one or more of each of the input words to exist (I suspect this is how the auto-generator works). If your posts do not contain all 3 words it will not therefore get returned.
You can remove the LIKE clauses for "Gift", though I think this will give you the same code as if you just searched for the first two words.
THEN comes before what is to be returned by the CASE function IE WHEN {true} THEN {return_this}
Hope this helps.
giving your query a nicer format (see below) it results that in the where clause you are filtering rows that have carousel in the title or content, watch AND gift.
The case statement only changes the ordering of the result, but if you build your query with those three words it will fetch rows with all those words.
SELECT SQL_CALC_FOUND_ROWS sosen_posts.ID FROM sosen_posts
WHERE 1=1
AND (((sosen_posts.post_title LIKE '%carousel%')
OR (sosen_posts.post_content LIKE '%carousel%')
)
AND ((sosen_posts.post_title LIKE '%watch%')
OR (sosen_posts.post_content LIKE '%watch%')
)
AND ((sosen_posts.post_title LIKE '%gift%')
OR (sosen_posts.post_content LIKE '%gift%')
)
)
AND (sosen_posts.post_password = '')
AND sosen_posts.post_type = 'wp_aff_products'
AND (sosen_posts.post_status = 'publish')
ORDER BY (CASE WHEN sosen_posts.post_title LIKE '%carousel watch gift%' THEN 1
WHEN sosen_posts.post_title LIKE '%carousel%' AND sosen_posts.post_title LIKE '%watch%' AND sosen_posts.post_title LIKE '%gift%' THEN 2
WHEN sosen_posts.post_title LIKE '%carousel%' OR sosen_posts.post_title LIKE '%watch%' OR sosen_posts.post_title LIKE '%gift%' THEN 3
WHEN sosen_posts.post_content LIKE '%carousel watch gift%' THEN 4
ELSE 5
END),
sosen_posts.post_date DESC
LIMIT 0, 3
Also note that LIMIT 0 will return an empty set.

MySQL LIke statement - multiple words

What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement? I cant find the right terms to make a search. If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement something like this.
SELECT *
FROM TABLE
WHERE SEARCH (LIKE %searchterm1%)
OR (LIKE %searchterm2%)
OR (LIKE %searchterm3%) ....
Try This. http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
SELECT *
FROM TABLE
WHERE SEARCH
REGEXP 'searchterm1|searchterm2|searchterm3'
Here's an example of a SQL SELECT statement that uses the LIKE comparison operator
SELECT t.*
FROM mytable t
WHERE t.col LIKE CONCAT('%','cdef','%')
AND t.col LIKE CONCAT('%','hijk','%')
AND t.col LIKE CONCAT('%','mnop','%')
Only rows that have a value in the col column that contains all of the strings 'cdef', 'hijk', and 'mnop' will be returned.
You specifically asked about the LIKE comparison operator. There's also a REGEXP operator that matches regular expressions. And the Full-Text search feature may be a good fit your use case.

Undocumented MySQL usage of the LIKE operator for multiple words

Can't seem to find documentation on a particular formation of SQL using the LIKE operator. Using MySQL, a typical query for multiple words using the LIKE operator may look like this:
SELECT * from table AS t WHERE t.col LIKE '%word1%' AND t.col LIKE '%word2%'
Although the following statement also works, the rows returned will vary depending on the order of the words in the query. For example:
SELECT * from table WHERE col LIKE '%word1%' '%word2%'
executes without the AND boolean, but with different results from:
SELECT * from table WHERE col LIKE '%word2%' '%word1%'
My question is, what is actually happening when using this formation of the query instead of using boolean?
From the manual:
Quoted strings placed next to each other are concatenated to a single
string. The following lines are equivalent:
'a string'
'a' ' ' 'string'
So, what's happening is that '%word1%' '%word2%' is being interpreted as '%word1%%word2%'

SQL string matching

I use a string for store the days of the week, something like this:
MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).
I don't know how to do this in SQL (MySQL).
use LIKE with %-wildcard between the single characters:
SELECT * FROM table WHERE column LIKE '%M%F%';
note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).
if the characters can be in random order, you'll have to built a query like:
SELECT * FROM table WHERE
column LIKE '%M%'
AND
column LIKE '%F%'
[more ANDs per character];
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'
Learn more:
http://www.sqllike.com/
Can you not just say
SELECT * FROM blah WHERE weekday LIKE "%MF%"