Display output of MySQL query in one line/ horizontally - mysql

I am validating the results of a MySQL database that I created and, for that, I need some screenshots.
For example, the following query:
select distinct run_ID
from ngsRunStats_FK.failedRuns
where reason_fail regexp 'cannot populate readsInfo'
will return (output from the terminal)
But as we can see, the screenshot is quite too long.
Is there a way to, instead of display the output as a (vertical) column, to display only its values horizontally (e.g. like in a python list)?

Try using GROUP_CONCAT:
SELECT GROUP_CONCAT(run_ID ORDER BY run_ID) AS run_ID_values
FROM ngsRunStats_FK.failedRuns
WHERE reason_fail REGEXP 'cannot populate readsInfo';
Side note:
If you really want to match the three keywords cannot populate readsInfo anywhere inside a larger text within the reason_fail column, then consider using word boundaries with REGEXP:
WHERE reason_fail REGEXP '[[:<:]]cannot populate readsInfo[[:>:]]';

Related

Insert Row in between query result or using google app script

So I have a set of data here, example below
and I am trying to get a result similar to this
I am thinking of using query, and I found this query formula:
=query({Sheet1!$A$1:$A;Sheet1!$B$1:$B;Sheet1!$C$1:$C},"select * where Col1 <>'' ",0)
but this only applies in transforming wide to long dataset
Is there a way to do it using query? or even an app script?
What you can do is to add a set of values with only the first two words. For that you can use:
=INDEX(SPLIT(UNIQUE(IFNA(REGEXEXTRACT(A:A,"(.+ .+) "))&"¿¿"),"¿",1,0))
PS: the two ¿¿ are for adding the extra columns. If you actually have more columns, add more ¿ signs -- (you don't need to do this step, I'm just showing you the process)
Then, with the query:
=QUERY({A:C;INDEX(SPLIT(UNIQUE(IFNA(REGEXEXTRACT(A:A,"(.+ .+) "))&"¿¿"),"¿",1,0))},"Select * where Col1 is not null order by Col1")
You'll get something like this in the right:
Then, with conditional formatting you can get something like this:
=($I1<>"")*($J1="")*($K1="")
PS: adapt the actual columns

SELECT Showing me a list with the name of the column and not the data inside

I have quite a strange problem, I'm very new to SQL and I was doing a free course in SQL
I'm actually learning the "SELECT" command.
So, I create my database, create some table, put some data in it with the "INSERT INTO" command
And now I want to select some data in it, but I have a strange bug (not an error) when i do
SELECT * FROM aliment;
everything work like it's supposed to do, but when I do
SELECT 'nom','calories' FROM aliment;
Something strange happens.
Instead have a list of all the specific data i'm looking to get i just get a list with all the data replaced by the name of the columns.
Here 2 screen to show you what's happens:
[2
I know one it's from the terminal(and yes it's not mine but from a video) and mine is from the software, but it's still had to work no?
You have a typo in your SQL. Use backticks (on the same key as ~ on a US keyboard) around your column names, not '. Using a single quote (an apostrophe) makes it a literal value, not a column name.
SELECT `nom`,`calories`FROM aliment;

sphinx search return match and surrounding characters

In Sphinx (using mysql connection), how can I match for a term and also get, let's say 5 characters before and after the match?
For example: a row contains this is a word in a sentence.
When I query: SELECT term FROM table WHERE MATCH('"word*"')
I would want to see is a word in a s returned. Is this possible?
Edit Using #barryhunter's helpful answer below, now trying to figure out how to fit this into my query:
SELECT field1,field2,SPHINX_SNIPPETS(field3,indexName, "word") as snippet FROM tableName
Thats what CALL SNIPPETS is designed for. Although its counted in words, not charactors.
http://sphinxsearch.com/docs/current/sphinxql-call-snippets.html
CALL SNIPPETS('this is a word in a sentance','index1','word', 2 AS around, 5 as limit_words);
would get back
... is a word in a ...
add '' as chunk_separator is dont want the ellipsis
Edit To add: then if want to build the snippet during the search query (not as a seperate CALL query), can use SNIPPET() function in the intial select
http://sphinxsearch.com/docs/current.html#sphinxql-select

Isolate an email address from a string using MySQL

I am trying to isolate an email address from a block of free field text (column name is TEXT).
There are many different variations of preceding and succeeding characters in the free text field, i.e.:
email me! john#smith.com
e:john#smith.com m:555-555-5555
john#smith.com--personal email
I've tried variations of INSTR() and SUBSTRING_INDEX() to first isolate the "#" (probably the one reliable constant in finding an email...) and extracting the characters to the left (up until a space or non-qualifying character like "-" or ":") and doing the same thing with the text following the #.
However - everything I've tried so far hasn't filtered out the noise to the level I need.
Obviously 100% accuracy isn't possible but would someone mind taking a crack at how I can structure my select statement?
There is no easy solution to do this within MySQL. However you can do this easily after you have retrieved it using regular expressions.
Here would be a an example of how to use it in your case: Regex example
If you want it to select all e-mail addresses from one string: Regex Example
You can use regex to extract the ones where it does contain an e-mail in MySQL but it still doesn't extract the group from the string. This has to be done outside MySQL
SELECT * FROM table
WHERE column RLIKE '\w*#\w*.\w*'
RLIKE is only for matching it, you can use REGEXP in the SELECT but it only returns 1 or 0 on whether it has found a match or not :s
If you do want to extract it in MySQL maybe this other stackoverflow post helps you out. But it seems like a lot of work instead of doing it outside MySQL
Now in MySQL 5 and 8 you can use REGEXP_SUBSTR to isolate just the email from a block of free text.
SELECT *, REGEXP_SUBSTR(`TEXT`, '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})') AS Emails FROM `mytable`;
If you want to get just the records with emails and remove duplicates ...
SELECT DISTINCT REGEXP_SUBSTR(`TEXT`, '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})') AS Emails FROM `mytable` WHERE `TEXT` REGEXP '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})';

return distinct text which is longtext format (but not returning the repeated text)

I have a simple scenario as follow:
I have 2 column : 1) id and 2) text(which is long text format)
I use the simple query to extract all info from mysql as follow:
select id,text from dbtest
but the problem is for different id I might have the same text but in retrieval time I do not want to return rows with the same text again and again so I do not want to return the repeated texts,I tried to use distinct but it was not working,
How can I do that , any idea?
One option is to use user-defined variables:
select id,
#text:=if(#text=text, '', text) text
from dbtest, (select #text:='') t
order by text
SQL fiddle demo
I would generally recommend doing this on the application side rather than the database though.
Distinct works on all selected columns. You must use a GROUP BY:
SELECT id,text FROM dbtest GROUP BY text
So I have two questions, can you post the code you are using for us? And are some of the fields for the text empty?
The query might be detecting a "Null" kind of event in which if the field is empty, it repeats the last one because the variable might not be getting unset. In which case you might just want to unset your variables at the end of your (I'm assuming) while loop.