Get distinct RegEx Matches on mySQL - mysql

I need some help from the RegEx and SQL nerds. ^^
I've a comment field in a table, which content looks like this:
What I need is a DISTINCT list of all user names - eg.
b.willis
p.fox
g.clooney
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`comment` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `comments` (`id`, `comment`) VALUES
(1, 'test [name](p.fox)[/name]'),
(2, 'another test [name](p.fox)[/name]'),
(3, 'lalala [name](b.willis)[/name]'),
(4, 'lulu [name](g.clooney)[/name]');
Thx!

One way to approach this is using substring_index(). Assuming each comment has this structure and only has one name:
select distinct substring_index(substring_index(comment, '[name](', 2), ')[/name]', 1) as name
from comments

See the thread MySQL - Return matching pattern in REGEXP query
Also check How to do a regular expression replace in MySQL? and the referenced UDF implementation of RegExp. Take a look on the implementation of the UDF REGEXP_SUBSTR

Related

Is it possible in mysql to dynamically translate IDs stored in a string?

I'm importing comment data in mysql from one database to another where some data may already exist. Relevant here, our comments table has a column display_text of type text. Inside this column we have mentions to other users stored in the format "#{{"user":id}}#" where id is a number representing the user's id in another table. It is possible for a single comment to have multiple mentions needing updated ids as well.
Since these users may already exist in the new database, part of our import process populates an id_translations table with old_id and new_id columns mapping the user's id in the source database to their id in the new database. Is it possible to write a mysql UPDATE statement to find the format of our mentions, and replace only the id with the new id?
I already have a method for selecting only the comments that will need ids updated, so statements can be written as though they would update the entire table.
MySQL version 5.6. Here's a sample of what my tables look like.
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(6) unsigned NOT NULL,
`display_text` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `id_translations` (
`old_id` int(6) unsigned NOT NULL,
`new_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`old_id`, `new_id`)
);
INSERT INTO `comments` (`id`, `display_text`) VALUES
(1, 'Hey #{{"user":12}}#, look at this'),
(2, 'Thats pretty cool'),
(3, '#{{"user":41}}# could you take a peek at this?');
INSERT INTO `id_translations` (old_id, new_id) VALUES (12, 100), (41, 101);
The goal would be for comments to look like:
1, 'Hey #{{"user":100}}#, look at this'
2, 'Thats pretty cool'
3, '#{{"user":101}}# could you take a peek at this?'
CREATE PROCEDURE replacing ()
BEGIN
REPEAT
UPDATE comments
SET display_text = CONCAT( LEFT(display_text, LOCATE('#{{"user":', display_text) - 1),
CHAR(0),
( SELECT new_id
FROM id_translations
WHERE old_id = 0 + SUBSTRING(display_text FROM LOCATE('#{{"user":', display_text) + 10)),
CHAR(1),
SUBSTRING(display_text FROM LOCATE('}}#', display_text) + 3))
WHERE LOCATE('#{{"user":', display_text);
UNTIL !ROW_COUNT() END REPEAT;
UPDATE comments
SET display_text = REPLACE(REPLACE(display_text, CHAR(0), '#{{"user":'), CHAR(1), '}}#');
END
DEMO fiddle

Query on "text" field in MySQL not working

I have a MySQL Query that looks something like this,
SELECT dactivityid, saction, resolution FROM supactiv where resolution <> ''
Now this returns results where resolution still has '' values in it.
My aim is to only show results, which actually have a valid resolution value and so are not ''.
Resolution is a "text" field in MySQL. Is there any way to fix this query so that it respects the condition in the query?
I have created below table and inserted below records and it worked perfectly.
------------------------------------------
CREATE TABLE IF NOT EXISTS `supactiv`
(
`dactivityid` int(11) NOT NULL AUTO_INCREMENT,
`saction` enum('A','B','C','D') NOT NULL,
`resolution` text NOT NULL,
`createdon` date NOT NULL,
`updatedon` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`dactivityid`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
------------------------------------------
INSERT INTO `supactiv` (`dactivityid`, `saction`, `resolution`, `createdon`, `updatedon`)
VALUES (1, 'A', '', '2015-07-02', '2015-07-02 17:51:03'),
(2, 'B', 'test', '2015-07-02', '2015-07-02 17:51:03'),
(3, 'C', '', '2015-07-02', '2015-07-02 17:51:03');
------------------------------------------
I have created the above table and tried your query it's working absolutely fine. You can check your table type and other aspects.
Or create table SQL and data SQL so I can check and answer you best of mine.
Thank you

MySQL fulltext not returning any results when it clearly should be

I'm trying to create a fulltext search index across three columns but it's not returning any results even though it seems like it should. I've replicated the problem in a testtable with the following structure and data:
CREATE TABLE IF NOT EXISTS `testtable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `title` (`title`,`link`,`description`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
INSERT INTO `testtable` (`id`, `title`, `link`, `description`) VALUES
(1, 'mysql article', 'domain.com/article/1', 'This is an article about MySQL'),
(2, 'some other article', 'domain.com/article/mysql-article', 'This has mysql in the link but not the title'),
(3, 'my super mysql article', 'domain.com/mysql-link', 'the keyword is not in the description'),
(4, 'okay i''m searching for something', 'domain.com', 'and it''s not in the title or description'),
(5, 'mysql article', 'mydomain.com/mysql', 'mysql is definitely written in every field');
This is the query i'm using:
select * from `testtable` where MATCH(title, link, description) AGAINST('mysql')
The phrase mysql appears in at least one column in everything except row 4, while row 5 has the phrase mysql in every single column. So at the very least it should be returning row 5. But it's returning no results.
Can anyone offer an explanation as to why this is happening?
words that are present in 50% or more of the rows are considered common and do not match.
From the Doc
That means if you look for a word that most records contain, then it will be ignored in the search.

Invalid use of group function - MySQL using COALESCE

I want a database table with links that are used to generate the navigation on a website. For instance, the text 'Home' will link to 'http://example.com/home' and the text 'Twitter' will link to the Twitter URL, etc. I also wanted to be able to change the order in which the links are presented, hence the order column. I also want to be able to edit the links, that's why I'm using auto_incremented id's.
Now I want order to be unique, so my plan was to get the max of order and just add one. This is the query I'm using, but it will return: Invalid use of group function
INSERT INTO
`links`
(`id`, `order`, `text`, `html_text`, `link`, `html_link`)
VALUES
(NULL, COALESCE((MAX(`order`) + 1), 1), 'text', 'text (html)', 'url', 'url (html)');
My table is like this:
CREATE TABLE IF NOT EXISTS `links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order` int(11) NOT NULL,
`text` varchar(31) NOT NULL,
`html_text` varchar(63) NOT NULL,
`link` varchar(127) NOT NULL,
`html_link` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
How do I get a valid query doing just what I want?
Thanks in advance!
If you want to do it in one shot, you'll have to do an INSERT ... SELECT combination to get the value from the database and insert based on it;
INSERT INTO
`links`
(`id`, `order`, `text`, `html_text`, `link`, `html_link`)
SELECT
NULL, COALESCE((MAX(`order`) + 1), 1), 'text', 'text (html)', 'url', 'url (html)'
FROM `links`;
An SQLfiddle to test with.

INSERT statement for MySQL table

CREATE TABLE IF NOT EXISTS `MyTable` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO MyTable (ID,Name) VALUES (ID=4,Name='xxx')
or
INSERT INTO MyTable (Name) VALUES (Name='xxx')
The problem is that both INSERT statements produce the entry (4,0). Why 0 instead of "xxx"?
UPDATE: Primary key changed.
This should do the job :
INSERT INTO MyTable (ID, Name) VALUES (4, 'xxx')
I'm pretty sure it would be something like this, instead...
INSERT INTO MyTable (Name) VALUES ('xxx')
No need for the Name= part, since you've already specified which column you wish to insert into with the first (Name) definition.
Because the expression Name='xxx' is false, hence evaluates as zero.
You use the column=expression method use in on duplicate key update clauses as described here, not in the "regular" section of inserts. An example of that:
insert into mytable (col1,col2) values (1,2)
on duplicate key update col1 = col1 + 1
You should be using the syntax:
INSERT INTO MyTable (ID,Name) VALUES (4,'xxx')
Is that syntax of Name='xxx' valid? Never seen it before, i assume it is seeing it as an unquoted literal, trying to convert it to a number and coming up with 0? I'm not sure at all
Try this:
INSERT INTO MyTable (Name) VALUES ('xxx')
This is because you should mention the name of the column in the values part. And also because you do not define you primary key correctly (airlineID is not part of the field list)
CREATE TABLE IF NOT EXISTS `MyTable` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO MyTable (ID,Name) VALUES (4,'xxx')
INSERT INTO MyTable (Name) VALUES ('xxx')
Try this
INSERT INTO MyTable (ID,Name) VALUES (4,xxx)
For more Info just visit this link