MySQL extratvalue range, output and parsing issues - mysql

I'm learning MySQL. My query is follows:
mysql>select extractvalue(1,1111111111111111111); // 19 1's.
output:
`+-------------------------------------+
| extractvalue(1,1111111111111111111) |
+-------------------------------------+
| 1111111111111111111 |
+-------------------------------------+
1 row in set (0.00 sec)`
But for 20 1's
mysql>select extract(1,11111111111111111111);
+--------------------------------------+
| extractvalue(1,11111111111111111111) |
+--------------------------------------+
| -7335632962598440505 |
+--------------------------------------+
1 row in set (0.00 sec)
And the following show me different errors:
mysql> select extractvalue(rand(),5.5.28));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '.28))
' at line 1
mysql> select extractvalue(rand(),version());
ERROR 1105 (HY000): XPATH syntax error: '.28'
Can someone explain me?

I tried out these, it seems to work.
SELECT #rand := RAND();
select extractvalue(1,#rand);
However, extract doesn't seem to work and gives the error. Either way I have not managed to use extract or extractvalue to wrap rand(). Reason could be that extractvalue only returns CDATA. Your intention of application doesn't seem to be supported by this function. It's used on XPATH
for e.g.
Select allhttp://www.blablabla.com/error.php?id=null
and extractvalue(rand(),concat(0x3a,(
select concat(0x3a,username,0x3a,password) from users)))
Found this to be interesting: reference

Related

In MySQL 8.x the string function FROM_BASE64(str) returns result in hexadecimal form instead of a string

I have the following queries -
SELECT "Abc" AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
SELECT TO_BASE64("Abc") AS result;
+--------+
| result |
+--------+
| QWJj |
+--------+
SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
--binary-as-hex page in mysql dev site says -
To disable hexadecimal notation, use --skip-binary-as-hex
I tried the following but got error -
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) --skip-binary-as-hex AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc") --skip-binary-as-hex) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex) AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc" --skip-binary-as-hex)) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as-hex)) AS result' at line 1
In the same page they have said how can I use the USING utf8mb4 clause to get the result in case of CHAR() and CONVERT() functions but they have not stated anything about FROM_BASE64() function. Nevertheless, I tried it and got error -
SELECT FROM_BASE64(TO_BASE64("Abc") USING utf8mb4) AS result;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING utf8mb4) AS result' at line 1
I tried the UNHEX() function -
ELECT UNHEX(FROM_BASE64(TO_BASE64("Abc"))) AS result;
+----------------+
| result |
+----------------+
| 0x0ABC |
+----------------+
Clearly this is not the desired result as well as all are in capital. Here LCASE() wouldn't work as well as it will turn every word to lower case.
Even, this result is not in string as is evident from -
SELECT SUBSTRING(UNHEX(FROM_BASE64(TO_BASE64("Abc"))) FROM 4) AS result;
+----------------+
| result |
+----------------+
| 0x |
+----------------+
So the only option seems to be to disable -binary-as-hex
But I can't find out a way to do this.
Here as similar question is there in stackoverflow -
'FROM_BASE64' Function Returns Hex Value
But it is on MySQL Version 5.6.14. I am using MySQL Version 8.0.27 -
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
So my case is different.
The --skip-binary-as-hex option is to be used as an option to the mysql command when you open that from a shell prompt. It's not an option to be used within SQL syntax. See https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_binary-as-hex
That said, you can convert binary to strings even if binary-as-hex is enabled:
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT(FROM_BASE64(TO_BASE64("Abc")) USING utf8mb4) AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
1 row in set (0.00 sec)
You may need to use a different character encoding. Mine is utf8mb4, but yours may be different.

1064 error in MySQL Command line client when trying to view table but works fine in MySQL Workbench

So i created a database in MySQL Workbench and I am trying to view them on the MySQL Command Line Client because i want to do my queries there instead of the workbench.
Here's whats happening:
mysql> use policestation(crime);
Database changed
mysql> show tables;
+--------------------------------+
| Tables_in_policestation(crime) |
+--------------------------------+
| accused |
| case |
| complainant |
| investigation_officer |
| outcome |
| section_of_law |
+--------------------------------+
6 rows in set (0.04 sec)
mysql> select * from case;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'case' at line 1
The same query works fine in MySQL Workbech. Also it works for all other tables in Command Line Client.
It's b/c Case is a MySQL reserved keyword: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
You'll need to seround reserved keywords with tick marks not quotes.
Example:
SELECT * FROM `case`
Not the same as
SELECT * FROM 'case'
Also here is another helpful link:
Syntax error due to using a reserved word as a table or column name in MySQL

Unable to select table in mysql

This is mysql table:
+---------------------+
| Tables_in_myproject |
+---------------------+
| reds |
| zxy |
| abcd |
| release |
+---------------------+
4 rows in set (0.46 sec)
When I try to select or show release table it throws the following error:
mysql> select * from release;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release' at line 1
What am I doing wrong? I am able to select all the other tables.
release is a reserved keyword in MySQL and needs to be escaped by backticks.
select * from `release`;
release is a reserved word. use backticks arround or better change the table name.
select * from `release`;

CONTAINS Not Working in mysql

I am working with CONTAINS, but it is not woking in mysql.
mysql> SELECT * FROM MUTHU;
+---------------------+
| MY |
+---------------------+
| how is your studies |
| how are you there |
| hi there |
+---------------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM MUTHU CONTAINS ( MY, "how" );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '( MY,
"how" )' at line 1
I don't think MYSQL has a function called CONTAINS, try the following query:
SELECT * FROM MUTHU
WHERE MY LIKE '%how%';
I believe contains is used primarily in Xpath/Xquery. A similar function in mysql is INSTR(). The proper code is for using this is:
select * from MUTHU
where INSTR(MY, 'how') > 0;
you missed the Where condition,
try this:
SELECT * FROM MUTHU
Where MY CONTAINS ( MY,"how" );

To cast or not to cast?

I am developing a system using MySQL queries written by another programmer, and am adapting his code.
I have three questions:
1.
One of the queries has this select statement:
SELECT
[...]
AVG(mytable.foo, 1) AS 'myaverage'`,
Is the 1 in AVG(mytable.foo, 1) AS 'myaverage' legitimate? I can find no documentation to support its usage?
2.
The result of this gives me average values to 2 decimal places, why?.
3.
I am using this to create a temp table. So:
(SELECT
[...]
AVG(`mytable`.`foo`, 1) AS `myaverage`,
FROM
[...]
WHERE
[...]
GROUP BY
[...])
UNION
(SELECT
[...]
FROM
[...]
WHERE
[...]
GROUP BY
[...])
) AS `tmptable`
ORDER BY
`tmptable`.`myaverage` DESC
When I sort the table on this column I get output which indicates that this average is being stored as a string, so the result is like:
9.3
11.1
In order to get around this what should I use?
Should I be using CAST or CONVERT, as DECIMAL (which I read is basically binary), BINARY itself, or UNSIGNED?
Or, is there a way to state that myaverage should be an integer when I name it in the AS statement?
Something like:
SELECT
AVG(myaverage) AS `myaverage`, INT(10)
Thanks.
On your last question: can you post the exact MySQL query that you are using?
The result type of a column from a UNION is determined by everything you get back. See http://dev.mysql.com/doc/refman/5.0/en/union.html .
So, even if your AVG() function returns a DOUBLE, the other part of the UNION may still return a string. In which case the column type of the result will be a string.
See the following example:
mysql> select a from (select 19 as a union select '120') c order by a;
+-----+
| a |
+-----+
| 120 |
| 19 |
+-----+
2 rows in set (0.00 sec)
mysql> select a from (select 19 as a union select 120) c order by a;
+-----+
| a |
+-----+
| 19 |
| 120 |
+-----+
2 rows in set (0.00 sec)
Just for anyone who's interested, I must have deleted or changed my predecessors code so this AVG question was incorrect. The correct code was ROUND(AVG(myaverage),1). Apologies to those who scrathed their heads over my stupidity.
on 1.
AVG() accepts exactly one argument, otherwise MySQL will raise an error:
mysql> SELECT AVG( id, 1 ) FROM anytable;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1 )' at line 1
http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_avg
Just because I'm curious - what should the second argument do?