Long detailed listing in SELECT Queries - mysql

I've had a look and reviewed the MySQL syntax, but can't figure out the right query modifier.
When you perform a normal select query, if there are a lot of columns (or one or two wide columns) the output wraps around the screen making it hard to read.
+----------------------------------------+-----------+--------+-----------------------
-----------------+----------------------------------------+------+-------------------+
--------------+---------------------------+-----------------+-------------------+-----
-------------------+-----------------+-------------------+-----------+
I recall someone showing me a long time there was a way of coercing MySQL to output each column on its own line like so:
id: 123456
Short_Field: Boy it's short
A_Long_Field: This is quite long, wow, very lorum, much ipsum...
Can anyone tell me what I'd need to put in the follow query to replicate this behaviour:
SELECT * FROM TABLE WHERE COLUMN="value" LIMIT 1;

The mode you are looking for is called "vertical output" and it is available when using \G as the delimiter in the MySQL command line client.
SELECT * FROM `table` WHERE `column` = 'value' LIMIT 1\G
Documentation on MySQL command line options

Related

SQL MATCH ... AGAINST limits result to current year

I'm stumped by this:
Two commands:
SELECT Date,Sentence FROM exampletable;
SELECT Date,Sentence FROM exampletable WHERE MATCH (Sentence) AGAINST ("South" IN NATURAL LANGUAGE MODE );
The first gives me results for the entire database, beginning in 2013. I see that there's an entry sometime in 2018 that contains the word "South", so using a match against in the second command I know I should get at least one result pre 2020. However, the first result is from 2020-01-28 onwards.
This happens in all examples I try. Simply adding a match against limits my returned results to > 2020. Is there some database setting that I'm not aware of? Or just something plainly obvious?
Any help would be appreciated! (I'm using MySQL 5.7)
Your query has no order by. Have you tried something like this?
SELECT Date, Sentence
FROM exampletable
WHERE MATCH (Sentence) AGAINST ("South" IN NATURAL LANGUAGE MODE )
ORDER BY date;
Perhaps the earlier dates are just later in the result set.

How to retrieve truncated queries from sys.`statements_with_temp_tables` view?

I am running below query on MySQL 5.7.22 and I see that queries are truncated under query column of the table.
select * from sys.`statements_with_temp_tables`
where db = 'schema_name'
order by memory_tmp_tables desc;
Is there any way we can retrieve these queries in full?
I have been enoyed by this query truncation for a long time too. So I just finished a short research to finally get a solution...
So. This 'truncation' is controlled by #sys.statement_truncate_len option. You can set it to reasonably long value before you select from sys.statements_with_temp_tables (or any other table containing monitoring data):
SET #sys.statement_truncate_len = 1000;
select * from sys.`statements_with_temp_tables`;
You can read more on this in official doc:
https://dev.mysql.com/doc/refman/5.7/en/sys-sys-config.html
P.S. If you use MySql Workbench: do not forget to set 'Max.Field Value Length to display' limit in Edit - Preferences - SQL Execution (main menu)

Error: MySQL client ran out of memory

Can anyone please advise me on this error...
The database has 40,000 news stories but only the fields 'story' is large,
'old' is a numeric value 0 or 1,
'title' and 'shortstory' are very short or NULL.
any advice appreciated. This is the result of running a search database query.
Error: MySQL client ran out of memory
Statement: SELECT news30_access.usehtml, old, title, story, shortstory, news30_access.name AS accessname, news30_users.user AS authorname, timestamp, news30_story.id AS newsid FROM news30_story LEFT JOIN news30_users ON news30_story.author = news30_users.uid LEFT JOIN news30_access ON news30_users.uid = news30_access.uid WHERE title LIKE ? OR story LIKE ? OR shortstory LIKE ? OR news30_users.user LIKE ? ORDER BY timestamp DESC
The simple answer is: don't use story in the SELECT clause.
If you want the story, then limit the number of results being returned. Start with, say, 100 results by adding:
limit 100
to the end of the query. This will get the 100 most recent stories.
I also note that you are using like with story as well as other string columns. You probably want to be using match with a full text index. This doesn't solve your immediate problem (which is returning too much data to the client). But, it will make your queries run faster.
To learn about full text search, start with the documentation.

SQL Injection on BadStore

I'm trying to excercise on BadStore, for those who don't know it's a fake online store site which can be run on VM box, and offers a lot of security vulnerabilities.
One thing i'm trying to do is to apply sql injection on the search query.
When searching for "book", for instance, we see this:
So, i'm trying to show all the store items trying to search for 1=1' --, which will result with the query of:
SELECT itemnum, sdesc, ldesc, price FROM itemdb WHERE '1=1' --' IN (itemnum,sdesc,ldesc)
however, this not giving the expected outcome as I get the following error:
Any suggestions?
You realize that -- in MySQL acts as a comment for the rest of the line?
If this is what you are trying to do, commenting out the rest of the line, then as per the MySQL documentation, you need a space after the --.
I understand you are trying out MySQL injection, so try to type your query, and then after the query type ; -- Notice that there IS a trailing space.
TL;DR
Change
'1=1' --' IN
TO
'1=1' -- ' IN

Wordnet MySQL statement doesn't complete

I'm using the Wordnet SQL database from here: http://wnsqlbuilder.sourceforge.net
It's all built fine and users with appropriate privileges have been set.
I'm trying to find synonyms of words and have tried to use the two example statements at the bottom of this page: http://wnsqlbuilder.sourceforge.net/sql-links.html
SELECT synsetid,dest.lemma,SUBSTRING(src.definition FROM 1 FOR 60) FROM wordsXsensesXsynsets AS src INNER JOIN wordsXsensesXsynsets AS dest USING(synsetid) WHERE src.lemma = 'option' AND dest.lemma <> 'option'
SELECT synsetid,lemma,SUBSTRING(definition FROM 1 FOR 60) FROM wordsXsensesXsynsets WHERE synsetid IN ( SELECT synsetid FROM wordsXsensesXsynsets WHERE lemma = 'option') AND lemma <> 'option' ORDER BY synsetid
However, they never complete. At least not in any reasonable amount of time and I have had to cancel all of the queries. All other queries seem to work find and when I break up the second SQL example, I can get the individual parts to work and complete in reasonable times (about 0.40 seconds)
When I try and run the full statement however, the MySQL command line client just hangs.
Is there a problem with this syntax? What is causing it to take so long?
EDIT:
Output of "EXPLAIN SELECT ..."
Output of "EXPLAIN EXTENDED ...; SHOW WARNINGS;"
I did more digging and looking into the various statements used and found the problem was in the IN command.
MySQL repeats the statement for every single row in the database. This is the cause of the hang, as it had to run through hundreds of thousands of records.
My remedy to this was to split the command into two separate database calls first getting the synsets, and then dynamically creating a bound SQL string to look for the words in the synsets.