MySQL #1243 Unknown prepared statement handler (stmt) given to EXECUTE - mysql

I am following this tutorial on my installed version of MySQL, but it's throwing me an error:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(property_name = ''',
property_name,
''', value, NULL)) AS ',
property_name
)
) INTO #sql
FROM
properties;
SET #sql = CONCAT('SELECT item_id, ', #sql, ' FROM properties GROUP BY item_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I am pasting it on SQL editor in phpMyAdmin.
I followed the suggestion. No errors shown but here's the result:
SELECT item_id
,MAX(IF(property_name = 'color', value, NULL)) AS color
,MAX(IF(property_name = 'size', value, NULL)) AS size
,MAX(IF(property_name = 'weight', value, NULL)) AS weight
FROM properties GROUP BY item_id

If you have access to a MySQL command-line, I think you'll find your SQL code is fine (as long as #sql doesn't equal NULL) and the issue is with phpMyAdmin. Another idea is to wrap the code in a stored procedure and then CALL the procedure.

You need to remove the DEALLOCATE PREPARE stmt; from your query until after the query runs.
DEALLOCATE cancels the statement before it has a chance to run.

Try this in MySQL Workbench. I had the same issue in phpMyAdmin and I tried in Workbench and it works fine.

Related

How to Transpose Rows to Columns Dynamically in MySQL

As per the attached images, each orderId will have transactions and should come as output as shown in the second image.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when tiid = ''',
tiid,
''' then amount end) ' ,concat('T',tiid)
)
) INTO #sql
FROM
Test;
SET #sql = CONCAT('SELECT poid, ', #sql, '
FROM Test
WHERE amount is not null
GROUP BY poid');
PREPARE stmt FROM #sql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
I have used the above code but getting the output in different format as shown below.
Kindly help me to resolve this.
Thanks in Advance.

Transpose data from rows to column in mysql/mariadb

I have very limited knowledge in mySql yet i try to help myself by reading stuff over internet and experiment things. I have a situation where i need to pivot/transpose data from rows to column. I have prepared a set of statements which is working perfectly fine as SQL:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when field_name = ''',
field_name,
''' then field_value end) ',
field_name
)
) INTO #sql
FROM
wp_fluentform_entry_details;
SET #sql = CONCAT('SELECT submission_id, ', #sql, '
FROM wp_fluentform_entry_details
GROUP BY submission_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Now i want to save it as view in mysql but unable. Any guidance in right direction would be highly appreciated.

How do I to label a column header as a formatted date?

What I have:
I have the following query that gets me pretty much what I need:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date,"'")
)
) INTO #sql
FROM updates;
SET #sql = CONCAT('SELECT Action, ', #sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
And gives the following output:
What I need:
What I would like is for the Column headers to be in the following format examples:
day/month/year
04/10/2016
%d/%m/&Y
What I have tried:
I have tried a number of different alterations to my code (far too many minor changes to list here) but each time I seem to get an error, to which I am guessing is regarding the formatting/syntax of the query.
Below is one example I tried to show the wave of thinking I was on:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(Date = ''',
Date,
''', Description, NULL)) AS ',
CONCAT("'",Date_Format(Date, "'"%d/%m/%Y"'"),"'")
)
) INTO #sql
FROM updates;
SET #sql = CONCAT('SELECT Action, ', #sql, ' FROM updates GROUP BY Action');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
As you can see I tried to set the "AS" value (Column name) using the DATE_FORMAT snippet of code. However, when I run this, I get the following error after the "FROM updates;" and before the "INTO #sql":
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 '%m/%Y"'"),"'")
This in turn gives me the following error message after the PREPARE stmt FROM #sql:
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 'NULL' at line 1
If anyone has any idea how to help me with this or point me in the right direction, I will be very grateful!
This select concat(date_format(now(), '%d/%m/%Y')) gives me 11/10/2016.
This is working perfectly fine. I guess you should consider removing the double-quotes. That should do the trick.

pivot query doesn't execute on mysql

in reference to this [pivot article]
I managed to get this prepared pivot query
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS ',
name
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
and here's a live demo SQLfiddle
the problem is when I try to execute that on my data with the same schema using mysql(5.6.14 Win32 x86) I get an error.
Error
SQL query:
PREPARE stmt FROM #sql ;
MySQL said:
#1064 -
just an error code but no message..
I've read SO questions like this & this but the answers are the static way which won't work with unknown columns
first of all, is this even available in mysql?? .. any pointers are appreciated
Your schema may be the same, but your data is probably different. You may have a keyword, space, or something else in your values which is causing the issue. Try to wrap your alias in double quotes.
Here's my edit to your SQLFiddle. I added a line which outputs the dynamic SQL in case you need to examine what you get on your system with your data.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS "',
name, '"'
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
SELECT CONCAT(#sql);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Creating a Crosstab query with mysql

I am new to php and mysql and I am trying to create a crosstab query using the code below:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(IF(category = ''',category,''', duration,NULL))AS ',category)) INTO #sql
FROM tblnon_oeedata;
SET #sql = CONCAT('SELECT productionDay, ', #sql, ' FROM tblnon_oeedata GROUP BY productionDay');
PREPARE stmt FROM #sql;
EXECUTE stmt;
Everytime I run it I keep getting the error:
#1064 - 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 'Maintenance During Production,SUM(IF(category = 'Breakdowns', duration,NULL))AS ' at line 1
How do I solve this problem, i am really stuck. Thanks in Advance
Try this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(IF(category = ''',category,''', duration,NULL))AS ''',category, '''')) INTO #sql
FROM tblnon_oeedata;
SET #sql = CONCAT('SELECT productionDay, ', #sql, ' FROM tblnon_oeedata GROUP BY productionDay');
PREPARE stmt FROM #sql;
EXECUTE stmt;