MySQL Search Refinement (replace long regex with subquery) - mysql

I have a MySQL query
select query from HR_Health_Logs where query REGEXP 'CPU|MAC|PC|abacus|calculator|laptop|mainframe|microcomputer|minicomputer|machine';
Except that the regex is much longer, and contains many synonyms and misspellings.
I need to cut this short and have a table with all the synonyms and misspellings, so that I can avoid this very long query. So I'm looking for something like
select query from HR_Health_Logs where query REGEXP '**HAVE A TABLE WITH ALL MY SYNONYMS AND MISSPELLINGS SEARCHED HERE**';

How about the ANY function ?
select query from HR_Health_Logs where query REGEXP ANY (SELECT spell FROM misspelled WHERE correct = 'masturbate' ) ;

SELECT query
FROM HR_Health_Logs l, synonym s
WHERE l.query = s.synonym

SELECT query
FROM HR_Health_Logs
WHERE query IN (
SELECT synonym AS query
FROM synonyms_table
WHERE word = 'masturbation'
UNION
SELECT misspelling AS query
FROM misspellings_table
WHERE word = 'masturbation'
)
Assuming your synonyms and misspellings are in two separate tables. Otherwise you'll only use one of the subqueries and drop the UNION.

Related

MySQL When to use quotation after SELECT AS

After reviewing the use of SELECT in mysql, I found after as, sometimes without single quotation and sometimes has.
For example:
SELECT * AS DAY
compare to:
SELECT * AS 'Cancellation Rate'
So when to use single quotation after SELECT AS?
for composite name eg: Cancellation Rate.. use backtics not quotes
select my_col_name as `Cancellation Rate`
from my_table
The proper syntax would be something like:
SELECT column_name AS colname FROM table_name
As mentioned in the comment, you cannot not alias a 'select all', which is what * represents. It selects ALL columns from your table.
You can also alias a table's name, like:
SELECT * FROM employees e WHERE column_name = 1;
When you alias a table's name, it can be easier to read in larger and more complex queries such as Joins.
You can get a better idea of all the possibilities by exploring this page https://dev.mysql.com/doc/refman/8.0/en/select.html, plenty of fairly easy to follow examples.

SELECT Mysql with combination of two where

I want select 2 Rows from the mysql table with additional "LIKE" and "AND" clauses..
Wit like clause I want to find only word starting with "a%"..
But I can't find the syntax error. Can you give me some hints.??
SELECT word,description
FROM word
WHERE(`language` = CONVERT( _utf8 'Tedi' USING armscii8 ) AND like 'a%') AND `visible` =1
many thanks in advance.
Regards,
Koko
The syntax error is a missing expression before the LIKE comparison operator.
We'll have to guess what expression you wanted to do the comparison operation on, so I'll just choose the first column from the SELECT list, to demonstrate:
SELECT w.word
, w.description
FROM word w
WHERE w.language = CONVERT( _utf8 'Tedi' USING armscii8 )
AND w.word LIKE 'a%'
AND w.visible = 1
The predicates in the WHERE clause specify the criteria that a row has to satisfy before it will be returned, it doesn't care whether that's zero rows, two rows or a brazilian rows.

Count(1) and the Like statement Best use/Performance

Is there a better way of doing the below sql query I am not sure if the Like statement is the best option as the location column only contains exact matches.
INSERT INTO test_reports (Table_Name, Total_Count)
SELECT "table1", COUNT(1)
FROM table1
WHERE location LIKE 'birmingham'
There's no wildcards in your search string. Without wildcards, like is exactly the same as =.

Mysql multiple row index

I have a following query in mysql.
SELECT
*
FROM
Accounts AS a
WHERE
('s' IS NULL OR (a.FirstName LIKE CONCAT('s','%') OR
a.LastName LIKE CONCAT('s','%') OR
a.FullName LIKE CONCAT('s','%')
)
)
How Should I put indexes for the table?
p.s.
's' is actually a variable in stored proc, so 's' IS NULL and concat are necessary.
First of all, just a quick suggestion: do not use concat if you don't have to. Your query can be rewritten as ('s' is NULL) is always FALSE so you can will always get all rows based on the second condition anyway:
SELECT
*
FROM
Accounts AS a
WHERE
a.FirstName LIKE 's%' OR
a.LastName LIKE 's%' OR
a.FullName LIKE 's%'
Indexes that might help, but no necessarily will are:
create index idx_01 on accounts(FirstName);
create index idx_01 on accounts(LastName);
create index idx_01 on accounts(FullName);
You can also consider a FULL TEXT SEARCH index for your table.
's' IS NULL is always false
Is there any reason you're using CONCAT('s','%') instead of 's%'?
Try a composite index on (FirstName, LastName, FullName), although it might not work really well for (VAR)CHARs (or even at all it seems)
Since #3 didn't work, I can only refer you to MySQL manual now. THere's a bit about using how MySQL uses indexes with LIKE here
FOR you full text indexing is also an option
add fulltext index for 3 fields then
use
MATCH() AGAINST() syntax
Eg
SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('superb catch' IN BOOLEAN MODE);

Search MySQL table for a kind of results

How can I search within a MySQL table for results ending in anything except ".jpg"?
Thanks.
You don't need to involve regular expressions, you can just do:
SELECT my_fields
FROM my_table
WHERE my_field NOT LIKE '%.jpg'
SELECT *
FROM mytable
WHERE myfield NOT RLIKE '\\.jpg$'