Query on "text" field in MySQL not working - mysql

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

Related

How to get auto-increment PK on a multi-row insert in MySql

I need to get back a list of "affected" ids when inserting multiple rows at once. Some rows might already be there, resulting in an update instead.
DDL:
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`email` varchar(100) NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`update_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
UNIQUE KEY `email` (`email`)
)
Query:
INSERT INTO users (id, email, is_active)
VALUES (NULL, "joe#mail.org", true),
(NULL, "jack#mail.org", false),
(NULL, "dave#mail.org", true)
ON DUPLICATE KEY UPDATE
is_active = VALUES(is_active)
There is a UNIQUE constraint on email.
From what I gathered, LAST_INSERT_ID() would only give me first generated id of the batch. But I wouldn't know how many inserts/updates really took place.
The only way I could come up with is to follow with a second SELECT statement:
SELECT id
FROM users
WHERE email IN ("joe#mail.org", "jack#mail.org", "dave#mail.org")
Is there a better way?

How to put the sum of a cell at a cell in another table mysql workbench

Maybe the title is a little confusing but I will try to explain. I am trying to simulate the database of a telephone company. Among the tables I created, my interest is in these 3 tables.
CREATE TABLE `test`.`number` (
`MSISDN` BIGINT(10) NOT NULL COMMENT '',
`CUSTOMER_NUMBER` INT NOT NULL COMMENT '',
PRIMARY KEY (`MSISDN`) COMMENT ''
);
CREATE TABLE `test`.`bill_msisdn` (
`BILL_ID` INT NOT NULL COMMENT '',
`MONTHLY_FEE` DECIMAL(10 , 2 ) NOT NULL COMMENT '',
`CHARGES` DECIMAL(10 , 2 ) NULL DEFAULT 0 COMMENT '',
`TOTAL_BILL` DECIMAL(10 , 2 ) NOT NULL COMMENT '',
`MSISDN` VARCHAR(45) NOT NULL COMMENT '',
PRIMARY KEY (`BILL_ID`) COMMENT ''
);
CREATE TABLE `test`.`bill_customer_number` (
`BILL_ID_CUSTOMER` INT NOT NULL COMMENT '',
`CHARGE_DATE` DATE NOT NULL COMMENT '',
`TOTAL_BILL` DECIMAL(10 , 2 ) NOT NULL COMMENT '',
`CUSTOMER_NUMBER` INT NOT NULL COMMENT '',
PRIMARY KEY (`BILL_ID_CUSTOMER`) COMMENT ''
);
I filled these 3 tables with a little data to see if it works.
For the first table I wrote the following because I wanted to have 2 customers, the first with 2 numbers and the second with only one:
INSERT INTO number VALUES (6944747844,1234567);
INSERT INTO number VALUES (6944747845,1234567);
INSERT INTO number VALUES (6944747846,7654321);
For the second table I wrote:
INSERT INTO bill_msisdn VALUES (12345,10.50,0,MONTHLY_FEE+CHARGES,6944747844);
INSERT INTO bill_msisdn VALUES (12346,11.5,5.22,MONTHLY_FEE+CHARGES,6944747845);
INSERT INTO bill_msisdn VALUES (12347,3.10,9.02,MONTHLY_FEE+CHARGES,6944747846);
The MONTHLY_FEE+CHARGES i entered at TOTAL_BILL basicaly sums MONTHLY_FEE and CHARGES automatically.
What I do not know how to manage is at the third table (bill_customer_number)
I wrote:
INSERT INTO bill_customer_number VALUES (12345678,'2015-09-15',,1234567);
INSERT INTO bill_customer_number VALUES (12345679,'2015-09-13',,7654321);
leaving the TOTAL_BILL column, empty.
My question is if there is a way, to automatically calculate the TOTAL_BILL of each customer based on how many MSISDN's has.
For example customer 1234567 has two numbers ( 6944747844 and 6944747845). How can I fill automatically the column TOTAL_BILL of this line
INSERT INTO bill_customer_number VALUES (12345678,'2015-09-15',,1234567);
May be with help of subqueries?
INSERT INTO bill_customer_number VALUES (12345678,'2015-09-15',
( SELECT SUM(TOTAL_BILL) FROM bill_msisdn WHERE MSISDN = 1234567 )
,1234567);

Get distinct RegEx Matches on 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

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.