Comparing 2 sets of words in MYSQL - php

I am creating a query where in i would need to compare 2 sets of words. I need to compare a.error_description with b.KE_TAGS which both contain sets of words.
Example of text:
a.error_description = "LockResource engine call failed in script GLB_LOCK..."
b.KE_TAGS = "LockResource, GLB_LOCKRESOURCE_100_PS"
Both columns contain the word "LockResource".
Here is my Query below:
The expected result should show the KE_ID based on the KE_TAG "LockResource, GLB_LOCKRESOURCE_100_PS" for the error_description which contains "LockResource engine call failed in script GLB_LOCK..."
Select a.error_id, a.job_ID, a.error, a.error_description,
CONCAT('%',b.KE_TAGS,'%'), b.KE_TAGS, b.KE_ID
from errors_input as a right join knowledge_entry as b
on a.error_Description like CONCAT('%',b.KE_TAGS,'%') -- issue is in this line
WHERE a.error like '%BB_RAISEERROR_100_PS%'***
Hope you can help. thanks!

Related

how to get from mysql using in same sequence using laravel?

I have this record in my database
| 29 | Mac 190:193:194:195:196:197:198:199:200 |
the last column name is
path_address
if I have a string like this
190:193:194:195
I want MySQL to select the path_address that has same sequence
so I used this command
$query_data = $module_model::where('path_address','LIKE',"$structure%")->limit(7)->get();
where structure is :190:193:194:195 and it is working fine, but what is happening is that if I have string like this
190:193:194:195:197
it is also return mac as a result of this query ,
how can I set my query to bring the string that have same sequence only and stop if it has broken sequence , so the accepted sequence will be like this
190
190:193
190:193:194
190:193:194:195
190:193:194:195:196 and so on
but theses sequence must be rejected
190:194
190:193:194:196
190:193:196
190:193:194:195:196:198 etc .
For example, you may use
WHERE path_address LIKE '$structure%'
OR '$structure' LIKE CONCAT(path_address, '%')
First condition will return rows where path_address is not shorter than $structure, second - where it is not longer respectively.
fiddle

replacing specific strings in a SELECT sql query (PLUS) if contains LIKE '%text%' replace the whole result

Replacing strings in a select query to kill off bad characters that will be harmful to folder names I am creating. That part is working well.
SELECT cust_name =
REPLACE(REPLACE([cust_name],'.',''),'/','-')
FROM work,dbo.cust cust_name
WHERE work.cust_id_bill_to = cust_name.cust_code AND
work.job_id = '44514' AND
work.sub_job_id = '11'
(THAT) Works great. BUT NOW I have one customer result who has hundreds of duplicate customer names based on stores and franchises, need only 1 customer name returned. So, have to figure out how to add code that would replace the entire result where this string is used.. mcdonald
So if the result comes back with MCDONALDS STORE#4567 I need to replace that with simply MCDONALDS.
WHERE cust_name LIKE N %mcdonald%
No matter the full text result in this case, like an if statement combined with my replace statement and a wildcard to boot? Changes the hundreds of those results to a single answer ... if it has mcdonald in it, it just gives me MCDONALDS as a result. Any ideas how to put that together. TIA. JP
You can use CHARINDEX to find a string within another string. If the result is 0, then the string is not found.
Then you could combine that with a CASE statement.
select
case when charindex('mcdonalds', cust_name) = 0
then replace(replace([cust_name],'.',''),'/','-')
else 'mcdonalds'
end as customer name
from ...

MYSQL LIKE different behaviors for different languages, all tables utf8

I have three tables each with 3 columns, they are identical to each other. Only difference is se_words created with mysql GUI and the other two are created programmatically thrue sql inside mysql GUI.
se_words
en_words
es_words
-> id, name, user
I want to find words through the last 3 chars.
SELECT * FROM `es_words` WHERE word LIKE '%nar' // returns empty results (should be at least 500-1000+)
SELECT * FROM `en_words` WHERE word LIKE '%ate' // returns empty results (should be at least 500-1000+)
SELECT * FROM `se_words` WHERE word LIKE '%ens' // returns 1000+ results
The big problem is that en_words and es_words which are utf8 (same as se_words) always returns empty result.
I have changed it to latin, utf8_bin, utf8_unicode_ci, latin_spanish_ci etc but it doesnt matter. Still gives empty result
but
if I change the sql code to just 1 char and procent sign on both sides, the code gives a result.
SELECT * FROM `es_words` WHERE word LIKE '%a%' // returns 10000+ results
SELECT * FROM `en_words` WHERE word LIKE '%a%' // returns 10000+ results
How come same sqlcode different tables returns different result when all columns has multiply values which should be returned on LIKE search?
edit
Few of the words that should be returned upon LIKE search
es_words
abadernar,abaldonar,abanar,abandonar,abarrenar
en_words
aagate,abacate,abacinate,abalienate,abbate
From the code with trimmed variables
SELECT w.id, w.word, w.user
FROM en_words AS w
WHERE w.word LIKE '%gel' ORDER BY LENGTH(word) ASC LIMIT 0,500
Looks like an \r followed from the dictonary. Could only see it through an SQL DUMP. PHP would not show it at all.
I hope it solves the problem for you in the future who encounter it
(943, 'abridgments\r', 0),
(944, 'abrikosov\r', 0),
(945, 'abrikossov\r', 0),
(946, 'abril\r', 0),
(947, 'abrim\r', 0),
(948, 'abrin\r', 0),
(949, 'abris\r', 0),
(950, 'abristle\r', 0),

MySQL COUNT showing it's working out

I would expect the following to output the number "5", since there are 5 rows in the database with with item 68 and user 1. But instead I'm getting this output "12345".
$resultb4 = mysql_query("SELECT COUNT(comparedRating) FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");
while($rowb4 = mysql_fetch_array($resultb4)){
$countcomparedratings=$rowb4['COUNT(comparedRating)'];
}
echo $countcomparedratings;
What am I doing wrong?
The reason you are getting 12345 is because your query is returning 5 results and your code to output the count is simply outputting the concatenation of the returned array from the query.
Without understanding your database structure, I'm guessing that the reason you're getting the '12345' has something to do with your GROUP BY clause. Use a program like MySQLWOrkbench to connect to your database and test out your query before you include it into your code. It is a time saving technique to debug your queries.
Also, I would alias the COUNT value so that you simply refer to the alias when you refer to your column names.
SELECT COUNT(comparedRating) as ratingCount FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");

Mysql query help - count rows

I have two different tables trackingevent and link
Trackingevent looks like
eventName|eventStamp
SHARE_FACEBOOK|2011-01-20 14:05:40
SHARE_TWEET|2011-01-20 14:47:57
SHARE_FLICKR|2011-01-20 15:08:58
SHARE_STATION_LOGIN|2011-01-20 15:09:09
EMAIL_SHARE|2011-01-20 15:10:13
CONTEST_ENTRY:BLAH DATA|2011-01-20 15:10:13
CONTEST_ENTRY:BLAH DATA|2011-01-20 15:10:13
and link looks like
id|emailSub
6|1
7|0
8|1
9|0
And what I need to do is I need to count all the SHARE_FACEBOOK
and all the SHARE_TWEET
and all the SHARE_FLICKR
and all the SHARE_STATION_LOGIN
and all the COPNTEST_ENTRIES (Without the rest of the data after the :)
AND EMAIL_SHARE
and somehow combine those with the amount of emailSub (equals to 1)
and amount of id.
So I get a returned array sort of like
EMAIL_SHARE 77
SHARE_FLICKR 9
SHARE_FACEBOOK 105
SHARE_STATION_LOGIN 223
SHARE_TWEET 18
# of ID's
# of emailsub=1
CONTEST_ENTERIES 550
I can get the first part of it using
SELECT eventName, COUNT(*) FROM trackingevent GROUP BY eventName
But I am confused with how to get the # of ID's in the link the # of emailsubs=1 and the number of CONTEST_ENTRY:BLAH DATA (the blah data changes).
Would I have to do three different sql queries and combine the data? Or could I somehow combine them into a single one or?
This should give you the count of each type using the prefix
SELECT LEFT(eventName,INSTR(CONCAT(eventName,':'),':')) as prefix, count(*)
FROM trackingevent
GROUP BY LEFT(eventName,INSTR(CONCAT(eventName,':'),':'))
You'll have to answer the questions in the comments if this isn't what you were asking for

Categories