Navicat store mysql query result into variable - mysql

I'm using Navicat to manage a MySql database and I want to store a query result into a variable. I tried these two options and none of them is working, but they do work if used directly from MySql console.
First syntax:
SET #var =(SELECT id FROM client where `name` = "gerrard");
SELECT #var;
Second syntax:
SELECT id INTO #var FROM client where `name` = "gerrard";
SELECT #var;
Both of them shows null in the output.

Related

Get condition of WHERE clause from table

I have table1 which has only 2 columns: id and condition. For example:
id condition
--------------------
100 caption LIKE "%xyz%"
200 tag=5
300 color>153
...
The user sends an id to the server, and a specific select query must be run on table2 based on the condition of that id. For example, if id 100 is sent to server, then this query must be run:
SELECT * FROM table2 WHERE caption LIKE "%xyz%"
How to get the condition from table1 and run the query with that condition? I have already tried this:
SELECT * FROM table2 INNER JOIN table1 AS t1 ON t1.id=... WHERE t1.condition
However, I get the following warning with no result.
Warning: #1292 Truncated incorrect DOUBLE value: 'caption LIKE "%xyz%"'
This is possible in MySQL 5+ using prepared statements. You can create a procedure with a condition id as an argument:
DELIMITER //
CREATE PROCEDURE get_from_table2_by_condition_id(IN conditionId bigint)
BEGIN
SET #condition = NULL;
SELECT cond
INTO #condition
FROM table1
WHERE id = conditionId;
SET #sql = CONCAT('SELECT * FROM table2 WHERE ', COALESCE(#condition, 'FALSE'));
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
CALL get_from_table2_by_condition_id(100);
See a working example in this fiddle
I don't particularly recommend storing SQL code as you are describing.
But you have a client and a server. The solution is simply to query table1 for the condition and then construct the query that you want from that. This requires two queries.
You could set up a stored procedure inside the database that uses dynamic SQL for the same purpose.
There are several reasons why this is not desirable:
The code can introduce syntax errors which are quite hard to debug.
The code is subject to SQL injection, depending on the security features around table1.
Changes to the underlying tables might invalidate the conditions.
What is an alternative? One possibility is to create separate views for the different conditions. Or, just create one query and pass in parameters:
select t2.*
from table2 t2
where (caption like :caption or :caption is null) and
(tag = :tag or :tag is null) and
(color > :color or :color is null);
That is not possible, and if it where possible that would be a huge stored SQL injection security hole. To be able to do this, you will need something that will parse the stored condition and evaluate the condition on the fly. As far as I'm aware, MySQL has nothing built in for this.

MySQL variable is NULL after it was set

I set a variable in MySQL and in the next SELECT query it is NULL.
SET #test = 123;
SELECT #test;
When executing the SET statement, there is no error.
Do I have to enable/activate the usage of variables?
I run MySQL Server 4.1.

SELECT query in MYSQL stored function

I need to run following query in my stored function with variables
SELECT json_unquote(json_extract(value,'$."_key"')) INTO org_rank_value FROM preferences WHERE id=_id;
Here _key and _id are declared variables, but _key is not replaced with value since it is in quotes. Is there any way to build and execute above query?
Without more context about your database structure, this is how i interpreted your question.
1) you want to select a specific ID.
2) you want to change a parameter within your json-field based off of the ID.
3) you want to execute the query.
DECLARE #json NVARCHAR(MAX) = (SELECT org_rank_value FROM preferences WHERE id=_id)
SELECT * FROM OPENJSON(#json)
WITH (
yourKey varchar(200) '$.key' -- change to whatever your variables are.
) AS changeKey;
UPDATE preferences SET json_unquote(json_extract(value,'$."_key"')) INTO org_rank_value WHERE id=_id;
In MySQL, u can use
JSON_ARRAY_APPEND

Mysql select all existing tables from current database using stored procedure

I am still trying to solve this Mysql issue. I'm not a dba but need to figure out how to do it.
I need to run a select statement over all (50k) existing tables from current db.
Please note that union is not the correct way for me since I have more than 50k tables, I need to solve this with a loop.
So far, I have been trying with two approaches without success:
First: using a subquery and the information_schemma like:
Select *
from
(SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'my_db')
or
Select * from (show tables;);
Second: using a stored procedure like:
delimiter //
CREATE PROCEDURE get_all_tables()
BEGIN
DECLARE a varchar(100);
DECLARE cur1 CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'my_db';
OPEN cur1;
But neither is doing what I want.
It yields syntax or conceptual errors.
BTW: I already solve this using an external perl script performing a "show tables" and running a select withing a loop.
This is ok but I think this should be solved with pure mysql.
Any idea would be welcome.
Leo.
SELECT * FROM (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'my_db')
First of all, you can't do this in any implementation of SQL. The table name must be known by the query at prepare-time, not at run-time. You can't select from a string, you have to select from a table identifier.
It's the difference between these two queries:
SELECT * FROM MyTable -- identifier
SELECT * FROM 'MyTable' -- string value (this won't work)
By the way, most programming languages have a similar concept, of a function or class name being different from the name of that function or class. You can't execute a function by its name using the same syntax as you would execute it by its identifier.
<?php
$result = myFunction();
$result = 'myFunction'(); // nonsense
But some languages do have ways of getting around this:
<?php
$funcName = 'myFunction';
$result = $funcName();
In SQL, you can get around this limitation by using the table name as you build a new SQL query as a string. Then prepare and execute that SQL query string at runtime. This is called a dynamic SQL query.
But like the PHP example above of using a variable to store the name of the function, using dynamic SQL requires multiple steps. You can't combine the query to get your table names into the same query that uses the results.
You were on the right track with your stored procedure that opens a cursor for the set of table names. But you can't open cursors from dynamic SQL statements in stored procedures.
You can do what you need pretty easily using dynamic SQL in a scripting language like PHP or Python.

Remote mysql not executing query: how to make it simpler?

The query listed below is running fine on localhost but it's somehow hanging when executed remotely targeting my service provider database (both the PHP script and the SQL query in the phpMyAdmin console hang), although every chunk is returning the expected table when run individually.
What's wrong? Any suggestions on how to make it shorter or simpler and thus help the remote MySQL?
SELECT * FROM `Table1`
WHERE `Tag1` IN (
SELECT DISTINCT `Tag1` FROM `Table2`
WHERE `Tag1` NOT IN (
SELECT `Tag1` FROM `Table2` WHERE `Tag2` = '$keyWord'
)
)