Hey , i got this challenge , i got a MySQL DB table 1 with queries or text and table 2 with synonyms and misspellings as CSV [comma separated values]. Now i want to test if any query word in table 1 matches a synonym or misspelling in table 2 , then i would select them separately .
example :
table 1 row: "i am sick of HIV AIDS , what can i do?"
table 2 : HIV,AIDS,Cancer,TB,Chicken Pox ......
so this would be selected because at least there is a MATCH word in table 1 that matches a synonym in table 2.
On a MyISAM table:
SELECT *
FROM table1 com, table2 syn
WHERE MATCH (com.body) AGAINST(syn.list IN BOOLEAN MODE);
This will work even if your don't have a FULLTEXT index on com.body, but with a FULLTEXT index this will be super fast.
If you wrap your synonym lists into double quotes, like this:
"HIV", "AIDS", "chicken pox", "swine flu"
, only the whole phrases will be matched, not just split words.
select strings.text
from table1 strings
where exists (
select 1
from table2 sm
where instr(strings.text, sm.word) <> 0
)
Related
Table a looks like this:
uuid
----------------
0681a8ff0e114680b2688c538d92f3cb
148ba55922544c1b8ea8e7d43ffb8095
Table b looks like this:
uuid | username
-----------------------------------------------
0681a8ff-0e11-4680-b268-8c538d92f3cb | test123
148ba559-2254-4c1b-8ea8-e7d43ffb8095 | poop123
ac123b2a-6546-8979-3213-cb426aac426b | blabla
How do I select all values from table a with their respective username? Note that table a has the UUIDs without the hyphens, while table b has them with hyphens. (Both are VARCHARS though)
I know how to select the UUIDs from table a with the dashes added:
SELECT CONCAT_WS('-',MID(uuid,1,8),MID(uuid,9,4),MID(uuid,13,4),MID(uuid,17,4),MID(uuid,21,1000))
I also know how to join two tables based on a column, but I can't figure out how to add the dashes and do the join all in one query.
Just remove the hyphens in the on clause:
select . . .
from a join
b
on a.uuid = replace(b.uuid, '-', '');
Then, go back and figure out how to fix the original data. Your keys should have the same type and format for foreign key references.
I have Two tables in mysql database and I want to compare two columns each of them in a different table first table name "oc_product_option_value" has column:
product_option_value_id
20
21
22
23
50
100
and second table "oc_cart" has cuolomn
option
{"20":"228","24":"229"}
I want compare two table and select data from first table where "product_option_value_id" in second table.
I tried:
SELECT * FROM oc_product_option_value
WHERE product_option_id IN
(SELECT REPLACE(REPLACE(REPLACE(REPLACE(OPTION,'{',''),'}',''),':',','),'"','')
as `option` FROM `oc_cart`)
and no result
* columns Structure
"product_option_value_id" is int
"option" is TEXT
Heum.. Not sure that it will do what you expect but I think it's a first step:
1/ Returns only rows wich have a matching value in second table (oc_cart)
SELECT *
FROM oc_product_option_value acpoa
JOIN oc_cart acc ON acc.option REGEXP concat('"', acpoa.product_option_value_id, '"');
Be careful about naming a column with a reserved MySQL word (option)
EDIT :
2/ If you want to display a "result" (final_kept_column) after this comparison in order to display "value_id" or "option" even if there's no matching value in oc_cart, you can try someting like this :
SELECT *,
CASE WHEN acc.option IS NULL
THEN acpoa.product_option_value_id
ELSE 0
END AS final_kept_column
FROM oc_product_option_value acpoa
LEFT JOIN oc_cart acc ON acc.option REGEXP concat('"', acpoa.product_option_value_id, '"');
Hope this help
MySQL FULLTEXT index trouble.
There are:
Table with words like:
ID
WORDS
1
jfjdeur,rjjghfje,rioogr
2
fkjtifdfe,lerkr
3
kfrkriti
4
frlerkti,tykitriero,frorodfl,rfjkrjr
...
N
fjfjtiu,frkrker,fkdkri
MySQL FULLTEXT index on WORDS column.
Query like:
SELECT *
FROM `table`
WHERE match(`words`) against ('gjfjdeur,rioogr,tykitriero')
As the result returns:
ID
WORDS
1
gjfjdeur,rjjghfje,rioogr
(because contains gjfjdeur and rioogr)
4
frlerkti,tykitriero,frorodfl,rfjkrjr
(because contains tykitriero)
Is it possible to rewrite query to create additional column which will be contain found/matched word? Something like:
ID
WORDS
FOUND
1
gjfjdeur,rjjghfje,rioogr
gjfjdeur,rioogr
4
frlerkti,tykitriero,frorodfl,rfjkrjr
tykitriero
Maybe something like:
SELECT *, some_function_to_select_matched_range(`word`) as found
FROM `table`
WHERE match(`words`) against ('gjfjdeur,rioogr,tykitriero')
In realily, table.words consists of hundreds words and preg_match_all in php isn't a good solution to select the matched words from each found.
Consider a table category in a database, with column typeis. The datatype is varchar with values
typeis
------
2.5.1
12
1.1.1
11
letters12
.........
I want to write a query that only returns records with "." and numbers from 0-9
For example
2.5.1
1.1.1
So far, I have
select typeis from category where typeis
not in
(select typeis from category where typeis REGEXP '[^0-9 \.]+')
and typeis in
(select typeis from category where typeis REGEXP '^[0-9]+[\.]')
which seems to work. The problem is that it takes over 3secs for just 1500 records. I would like to make it simpler and faster with just one REGEXP, instead of having nested select
Try: ^[0-9]+\.[0-9]+(\.[0-9]+)*
This should match things starting with a number(s) including a dot somewhere in the middle, and ending with numbers, and as many of these patterns as it would like.
This is pretty easy & powerful:
^([0-9]+\.*)+
The query-time issue could be caused by no indexing. Try to index typeis column - if it is possible create an index of its full length. For example if you've varchar(255) create the index of 255 length, like:
create index index_name on table_name (column_name(length))
i have a varchar field with the content like these:
a,b,c,d
e,d,a,c
b,q,d,e
i need to do a query that select only the rows with the field that has elements equals with an input string.
ex
input: c,a
rows selected:
a,b,c,d
e,d,a,c
is possible without use the OR (field like '%a%' OR field like '%c%') ?
thanks
Yes, you can use...
WHERE
myField LIKE '%a%' AND myField LIKE '%c%'
However, this does sound like you've got a nasty field in your SQL.
Normally, you would use a link-table to specify this kind of information:
Table1
id
otherdata...
Table2
id
letter
LinkTable
Table1Id
Table2Id
So in LinkTable you would have many entries that link your records. So instead of storing 'a,b,c,d' in a field, you have four link records:
Table1Id Table2Id
1 1 (a)
1 2 (b)
1 3 (c)
1 4 (d)
Yes but you need a trick: Create a second column which contains the same value but sorted:
a,b,c,d
a,c,d,e
b,d,e,q
Now, you can query for %a%c%
This won't work if the query string can be a substring of any unwanted value. If this is the case, you need quotes:
"a","b","c","d"
"a","c","d","e"
"b","d","e","q"
and query for %"a"%"c"%