MySQL regex to return matching string only - mysql

I'm building a query to run in an Invision Power Board hosted site using the SQL Toolbox, unfortunately it seems a but limited and I've not been able to add custom SQL function so far. I've built a mutli-table query but for the sake of simplicity will simplify it here.
The following is a sample of the database and a query to find profiles which include an address in their About Me (field_11) profile using Regex. The results shows the entirety of the About Me and not just the matching string.
Is there a way to structure the query to return just the matching string found by regex without using a bespoke MySQL function?
SQL Database
CREATE TABLE core_pfields_content
(`field_11` LONGTEXT);
INSERT INTO core_pfields_content
(`field_11`)
VALUES
('<p> C++ eveloper</p> <p> AJ4ndBBLJZmRDVtC7JfGAVivhUXFDvWqKL </p>'),
('<p> Norwegian designer and illustrator working out of Oslo. </p>'),
('<p> Hello everyone, I am a Freelancer Photographer </p> <p> My portfolio: </p> <p> address: AW7fvzoEWAfiyyEwgXmgog525NCCeAxVmG </p>');
Query
SELECT field_11
FROM core_pfields_content
WHERE field_11 REGEXP '^.*([A][a-km-zA-HJ-NP-Z1-9]{26,33}).*';
Result
field_11
<p> C++ eveloper</p> <p> AJ4ndBBLJZmRDVtC7JfGAVivhUXFDvWqKL </p>
<p> Hello everyone, I am a Freelancer Photographer </p> <p> My portfolio: </p> <p> address: AW7fvzoEWAfiyyEwgXmgog525NCCeAxVmG </p>
http://sqlfiddle.com/#!9/02d1d/1

Related

Trouble putting together a SoQL query with Socrata

I have this query that I put together in MySQL in my test db and am trying to figure out how to port it over to the SoQL SODA API. Pretty much just trying to get a list of the unique business and associated details.
SELECT DISTINCT
(CAMIS), dba, boro, building, street
FROM
nyc_stuff.restauraunt_inspections
WHERE
BORO = 'BRONX';
This is the base url I'm using:
https://data.cityofnewyork.us/resource/xx67-kt59.json
I'm aware of how to use the clauses, but can't figure out how to add Distinct into the query.
I tired this:
https://data.cityofnewyork.us/resource/xx67-kt59.json?$select=DISTINCT%20(CAMIS)
But no luck...
You can accomplish this by using the $query parameter in SoQL. I would suggest:
https://data.cityofnewyork.us/resource/xx67-kt59.json?$query=SELECT%20distinct%20camis,%20dba,%20boro,%20building,%20street%20WHERE%20boro%20=%20%22BRONX%22
Thanks, Socrata Support

Remove html link from mysql database field with SQL

I have an old MySQL database and I need to remove different HTML links from description field.
So, for example after SQL execution on description with a following text:
Hello test World !
I need to get:
Hello World !
Please note that in my database HTML links are not the same and contain different addresses and texts.
Is it possible to do with MySQL SQL query and if so, could you please provide an example.
You can use a query like this. You only must change FIELDNAME to your fieldname and TABLENAME to your tablename. In your Sample there are one space behind HELLO ** and one before ** WORLD, so you have 2 spaces in the RESULT
SELECT
CONCAT(
SUBSTR(FIELDNAME,1,
INSTR(FIELDNAME,'<a href=')-1)
,
SUBSTR(FIELDNAME,
INSTR(FIELDNAME,'</a>')+4)
)
FROM YOURTABLE;
sample
SELECT
CONCAT(
SUBSTR('Hello test World !',1,
INSTR('Hello test World !','<a href=')-1)
,
SUBSTR('Hello test World !',
INSTR('Hello test World !','</a>')+4)
);
result
Hello World !

Having an issue with full text search returning empty results

I'm having an issue with trying to perform a full text search on a table in mysql. The table in question has the following create command:
CREATE TABLE IF NOT EXISTS `resume_contents`
(`resumeid` int(100) NOT NULL AUTO_INCREMENT,
`jobseekerid` varchar(255) NOT NULL,
`resumecontents` text,
PRIMARY KEY (`resumeid`),
UNIQUE KEY `jobseekerid` (`jobseekerid`),
FULLTEXT KEY `resumecontents` (`resumecontents`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
So basically just 3 columns with fulltext on one column only. Then when I try and do a full text search with the following command:
SELECT * FROM `resume_contents` WHERE MATCH (resumecontents) AGAINST('')
I get an empty result set everytime, no matter what words are in the against clause. What stupid error am I making here?
As an example here is a row I'm testing with, it has a resume sample from resume.com.
INSERT INTO `resume_contents` (`resumeid`, `jobseekerid`, `resumecontents`) VALUES (2, '14', 'Objectives\nI am looking for a position at This Company where I can maximize my programming skills, project management, leadership skills, and create new and exciting tools.\n\n\nSummary\nI have had 3 years experience in web design and development. I am fimilar with a lot of the languages and software used in creating projects for the web. Futhermore, I have great project management skills and also work well either alone or with a group. I have also worked on small projects and large public releases for large companies.\n\n\nEducation\nDesign Media \nCollege Humor, Seattle, WA\nGraduated: January 2008 \nGrade: College\n\nEmployment History\nDecember 2007 – Present: Web Designer \nCompany: ComStream\nSeattle, Washington\nWorked as team lead on many different client projects.\n\n\nProfessional Skills\nAdobe Photoshop – Expert\n\nAdobe Illustrator – Expert\n\nAdobe Dreamweaver – Advanced\n\nPhp – Advanced\n\n\nQualification/Certification\nJanuary 2006: New Media Design \nOrganization: School of Technology\nAward: \nSan Fransico\nLanguages\nCzech – Beginner\n\nVietnamese – Conversational\n\n\nImmigration / Work Status\nDecember 2041 – Permanent Resident - United States');
Then I search with :
SELECT * FROM `resume_contents` WHERE MATCH(`resumecontents`) AGAINST ('Company')
Or just about any word or phrase (any length, any word) which I know is in there... and still it returns an empty set.
I know it must be something simple I'm missing but I don't know what it could be.
What words? You know, that there are certain words that are not considered like "the" or "how", words that just appear to often to be meaningful. Then the words have to have a minimum length.
And finally!
In addition, words that are present in 50% or more of the rows are considered common and do not match.
I bet that is the case here. Read more about it here.

Query first sentence of an HTML paragraph field in SQL Server / Access front end

I have been trying to find how I can query the first sentence only of a paragraph within a field (HTML code) for SQL Server but I am unable to find how. I have found solutions for MySQL using the SUBSTRING_INDEX and I have also found solutions returning a certain number of words/characters but not using a specific delimiter.
My field is stored as HTML, an example is as follows:
<html><body>Enter the following page information.<br><br>
<b>Display #:</b> 1 [Automatically Populated]<br>
<b>Start Page: </b> 1 [Automatically Populated]<br>
<b>DCI Name:</b> DEMOG<br>
<b>Clinical Planned Event:</b> BASELINE1<br>
<font color="#0070C0">TAKE A SCREENSHOT</font>.<br>
</body></html>
In this example, I am hoping to only return/query "Enter the following page information" and not the rest of the paragraph. I'm assuming the HTML break might be the best delimiter as some sentences may end in a colon.
Thank you in advance! I hope I explained the scenario well enough.
I realize this is ugly as sin, but assuming that the first <br> is the end of the line, this should work in the SQL Server back-end:
DECLARE #x nvarchar(200)
SET #x = '<html><body>Enter the following page information.<br><br><b>Display #:</b>'
SELECT substring(#x,
(charindex('<br>', lower(#x)) - 1) -
(charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))))+2,
charindex('>', REVERSE(LEFT(#x, charindex('<br>', lower(#x)) - 1))) - 1
)
Basically, we find the last instance of > in the string before the first <br>, and then find the <br> at the end, and take the difference between the two for the length.
This could absolutely be written cleaner in a function, but I opted to go with pure T-SQL to avoid using functions.
A final note: You may not need the lower functions; my test database is case-sensitive, therefore the need to make the casing consistent.

Why MySQL return 0 result of in my query MATCH?

I am developing an application using Codeigniter 2.1.0 and MySQL. In my application I have a search feature. Here is code for search.
function get_results($query, $post_per_page,$current_page) {
$offset=($current_page-1)*$post_per_page;
if($current_page==1) {
$offset = 0;
}
$sql = "SELECT * FROM data WHERE MATCH ( source ) AGAINST (?) LIMIT ?,?";
$query = $this->db->query($sql, array($query, $offset, $post_per_page));
return $query->result_array();
}
Sample paragraph is
CodeIgniter User Guide Version 2.1.0 Getting Started With CodeIgniter Any software application requires some effort to learn. We've done our best to minimize the learning curve while making the process as enjoyable as possible. The first step is to install CodeIgniter, then read all the topics in the Introduction section of the Table of Contents. Next, read each of the General Topics pages in order. Each topic builds on the previous one, and includes code examples that you are encouraged to try. Once you understand the basics you''ll be ready to explore the Class Reference and Helper Reference pages to learn to utilize the native libraries and helper files.
I perform a query "codeigniter" and "User Guide" But this function returns 0 results on both queries. However words "codeigniter" and "User Guide" are exists in the sample paragraph. Please suggest some helpful code or ideas for this problem