How to use regexp - mysql

I have 'tags' table with columns (id, link). Link possible values:
id link
1 index
2 index/index
3 index/.*
when I got index, I need to select id 1. And:
index -> 1
index/index -> 2
index/test -> 3
I use something like this:
SELECT * FROM tags WHERE 'index/test' REGEXP link LIMIT 1
But it's return me id 1, if I remove LIMIT second row will be id 3. I need just full math - only id 3.

Also for regexp, the column identifier comes first, then the search value. When I get you right, you want to get id -> 3 whenever the search term is not index or index/index. So your regex could be something like
SELECT * FROM tags WHERE link REGEXP '[^index/index|^index]$'

Related

Highlight matched words MySQL FULLTEXT index

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.

found integer in text data with mysql

I have a database table,in one Column I have data Like:
id tags
1 1.2,2.2,22.2
2 20.1,30.1,45.2,46.0,55.3
3 1.3,6.1,7.2,9.4,10.2
I want to search ids having num 1 in tag's Column
Use LIKE operator to search the data
SELECT id
FROM tableA
WHERE tags LIKE '%1%'
Just another using INSTR when wildcard is on both sides in a LIKE search!
SELECT id
FROM tableA
WHERE INSTR(tags,'1') > 0 ;

mysql: special use of concat

I have table A and B with many to one associations (b contains fk_a). Let's assume the sample tables are as follows:
A:
id first
1 sample
2 sample
B:
id fk_a type value
1 1 som thing
2 1 oth other
3 2 som thing
4 2 oth any
I would like the "first" column in table A to be unique, and I would like to achieve it by having to:
desired A:
id first
1 sample-thing-other
2 sample-thing-any
Is it possible to use pure MYSQL to use UPDATE and CONCAT on table A to obtain desired update?
It would be easy if I had everything in one table, I could just write
UPDATE A
SET first = CONCAT(first, value)
but unfortunately I have many-to-one association and I am not sure if it is even possible in such case.
I do not have the instance of mysql, and not test it. use the group_concat
UPDATE A, (SELECT fk_a, GROUP_CONCAT(value SEPARATOR '-') as concat_value FROM B GROUP BY fk_a) AS t
SET A.first = CONCAT(A.first, '-', t.concat_value)
WHERE A.id = t.fk_a;
Group_Concat is your friend.
But may I remind you of first normal form?

Specific ordering SQL Command

Say I have a table similar to:
ID Name
1 Test
2 Contest
3 Fittest
4 Testament
Is there a MySQL query I could use with ordering to allow it to display a specific word first?
For example, users are searching for the word "Test". I have a statement similar to "SELECT * FROM table WHERE NAME LIKE '%Test%'". Could I display results to show things that START with Test begin first followed by everything else, while everything is still in alphabetical order.
So output would be:
Test
Testament
Contested
Fittest
Thanks.
This will put your words that begin with Test at the top, and sort those words plus the remainder of the list in alphabetical order..
SELECT * FROM mytable
ORDER BY CASE WHEN name LIKE 'test%' THEN 0 ELSE 1 END ASC, name ASC
SELECT * FROM table
WHERE NAME LIKE '%Test%'
order by case when name like 'test%' then 0 else 1 end

find elements of a varchar in another varchar

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"%