Running EXPLAIN on MySQL query is giving a syntax error - mysql

I have a basic query that looks like the following that's running slow, so I'm trying to figure out where there's missing indexes or other optimizations I can make:
drop table weights;
CREATE TABLE weights engine=myisam
SELECT 'K' AS STAT,
z.WEIGHT,
COUNT(*) AS SAMPLE,
round(AVG(z.FINAL_2Y),15) AS 2Y,
round(AVG(z.FINAL_3Y),15) AS 3Y,
round(AVG(z.FINAL_4Y),15) AS 4Y,
round(AVG(z.FINAL_5Y),15) AS 5Y,
round(AVG(z.FINAL_6Y),15) AS 6Y
FROM
( SELECT /* insert big query here */ ) z
GROUP BY WEIGHT;
This query is running awfully slow so I'm trying to make one simple change with the following:
drop table sp_weights_holding_table
CREATE TABLE sp_weights_holding_table engine=myisam
EXPLAIN SELECT 'K' AS STAT,
z.WEIGHT,
COUNT(*) AS SAMPLE,
round(AVG(z.FINAL_2Y),15) AS 2Y,
round(AVG(z.FINAL_3Y),15) AS 3Y,
round(AVG(z.FINAL_4Y),15) AS 4Y,
round(AVG(z.FINAL_5Y),15) AS 5Y,
round(AVG(z.FINAL_6Y),15) AS 6Y
FROM
( SELECT /* insert big query here */ ) z
GROUP BY WEIGHT;
As soon as I pop the EXPLAIN in, I get a syntax error. I'm using MySQL 5.6.13 on Amazon RDS. The error looks like the following:
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 'EXPLAIN SELECT 'K' AS STAT,
z.WEIGHT,
COUNT(*) AS SAMPLE,
r' at line 2

You must semicolon the end of the sql command.
Change to
drop table sp_weights_holding_table;
CREATE TABLE sp_weights_holding_table engine=myisam;
EXPLAIN SELECT 'K' AS STAT,
....

Use the EXPLAIN with the SELECT query alone without the first two lines for dropping/creating the table:
EXPLAIN SELECT 'K' AS STAT,
z.WEIGHT,
COUNT(*) AS SAMPLE,
round(AVG(z.FINAL_2Y),15) AS 2Y,
round(AVG(z.FINAL_3Y),15) AS 3Y,
round(AVG(z.FINAL_4Y),15) AS 4Y,
round(AVG(z.FINAL_5Y),15) AS 5Y,
round(AVG(z.FINAL_6Y),15) AS 6Y
FROM
( SELECT /* insert big query here */ ) z
GROUP BY WEIGHT;

Related

Update with nested select in from statement does not work mysql

Here is an error I don't understand:
mysql> UPDATE gp
-> SET gp.gpid = gp.new_gpid
-> FROM (
-> SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
-> FROM gp
-> ) gp;
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 'FROM (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
' at line 3
As far as I can tell nested SELECT in a FROM statement seems to be depreciated.
Am using mysql 8.0.21
Any help to make this query work would be greatly appreciated.
Thank you
EDIT 1:
What I am trying to achieve is to update the gpid column with row numbers instead of the actual AUTO_INCREMENT id, which contains gaps in between ids, the explanation in this post Change mysql auto increment id column value
The UPDATE... FROM ... syntax is not supported by MySql.
You can do it with a JOIN:
UPDATE gp
INNER JOIN (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY gpid) AS new_gpid
FROM gp
) t ON t.gpid = gp.gpid
SET gp.gpid = t.new_gpid;
See a simplified demo.
Your nested query is selecting from 'gp' but is then aliased as 'gp' it can't select from itself.

Why am I receiving "Unknown column 'xyz'" with WHERE clause on View SELECT query?

MySQL database (community) version: 5.6.27, Windows 7 Pro x64
I've just created this View:
DELIMITER $$
ALTER ALGORITHM=UNDEFINED DEFINER=`admin`#`%` SQL SECURITY DEFINER VIEW `vw_qb_assembly_component_info` AS (
SELECT
`qb_assembly_components`.`assembly_item_id` AS `assemblyId`,
`ai`.`name` AS `assemblyName`,
`qb_assembly_components`.`component_quantity` AS `componentQuantity`,
`qb_assembly_components`.`component_item_id` AS `item_id`,
`ci`.`name` AS `name`,
`ci`.`item_number_type` AS `item_number_type`,
`ci`.`type` AS `type`
FROM ((`qb_assembly_components`
JOIN `qb_items` `ai`
ON ((`ai`.`item_id` = `qb_assembly_components`.`assembly_item_id`)))
JOIN `qb_items` `ci`
ON ((`ci`.`item_id` = `qb_assembly_components`.`component_item_id`))))$$
DELIMITER ;
I am attempting to query the view for rows with a certain qb_assembly_components.assembly_item_id value. I've tried several variations of defining the column in the WHERE clause but always receive error:
Unknown column 'xyz' in 'where clause'
The following are the versions I've tried:
WHERE `qb_assembly_components`.`assemblyId` = 'RR-0T056'
WHERE `qb_assembly_components`.`assembly_item_id` = 'RR-0T056'
WHERE `assemblyId` = 'RR-0T056'
I'm stumped. I've googled a bit and found a few results that seem to suggest using the alias is the way to go (my last example in the above 3 examples) but it's not working.
What am I doing wrong?
If you are select from VIEWvw_qb_assembly_component_info``
then the where clause should refer to the view name (and not to the orginal table name)
WHERE `vw_qb_assembly_component_info`.`assemblyId` = 'RR-0T056'

MySql subquery error with delete statement

I'm learning sql from these wikibooks pages and I'm trying to answer the last question "Remove all boxes from saturated warehouses" using mysql syntax. The following is what I came up with on my own mainly using previous answers from the same page.
delete from Boxes as b
where b.Warehouse in (
select w.Code
from ( select *
from Warehouses) as w
where w.Capacity < ( select count(*)
from Boxes bb inner join Warehouses ww
on bb.Warehouse = ww.Code
group by bb.Contents) ) ;
The query for this question on the site is generating
this solution doesn't work with mysql 5.0.67
ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause
That's why I'm using multiple sub queries as a way to come around this mysql message. However, I have an error in my query and therefore it's not running.
Error Code: 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 'as b where b.Warehouse in ( select w.Code
from ( sel' at line 1
Any idea?
I assume Boxes and Warehouse are tables, you gave Boxes an alias and used it on the Warehouse table, that's probably why it didn't work. Maybe you should try with a query after 'delete from'.
I think your warehouses table returns from than one columns (since * is used). Replace * with the column u want to get the output from.

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
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 sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

SQL INSERT INTO statement.. Error, I am very new to SQL

I've searched everywhere for an answer and can't find anyone so I have resorted to making my own post. This is for a university assignment. I am using HeidiSQL.
This is my code
INSERT INTO
loan(patronCode,bookCode,loanDate,dueDate)
VALUES
(
SELECT
patron.patronCode
WHERE
patron.fname='Jacob' AND patron.lname='Simmons',
SELECT
book.bookCode
WHERE
book.bookTitle='The Agony And The Empathy',
'2014-09-10',
'2014-10-01'
),
(
SELECT
patron.patronCode
WHERE
patron.fname='Jacob' AND patron.lname='Simmons',
SELECT
book.bookCode
WHERE
book.bookTitle='Kevin Rudd: The Biography',
'2014-09-10',
'2014-10-01'
)
FROM
loan
INNER JOIN patron
ON loan.patronCode=patron.patronCode
INNER JOIN book
ON loan.bookCode=book.bookCode;
This is my error message:
SQL Error (1064): You have an error in your SQL synstax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT patron.patronCode
WHERE
patron.fname='Jacob' AND patron.lname='Si' at line 5
An insert statement needs to take the form of:
INSERT INTO TABLENAME(FIELDNAME1, FIELDNAME2...)
VALUES('HARDCODED_VALUE1', 'HARDCODED_VALUE2'...)
You have mixed this with the logic of an insert with a select statement. Which looks like this:
INSERT INTO TABLENAME(FIELDNAME1, FIELDNAME2...)
SELECT Column1, Column2...
FROM TABLENAME
You have 2 different inserts here. Perhaps they could be combined with a bit more information. Also, you dont indicate how patron and book are joined, probably they arent, so I've left the join out.
Here is the first one, the second can be patterned after it.
INSERT INTO loan(patronCode,bookCode,loanDate,dueDate)
SELECT patron.patronCode,book.bookCode,'2014-09-10','2014-10-01'
FROM patron, book
WHERE
patron.fname='Jacob' AND patron.lname='Simmons'
and book.bookTitle='The Agony And The Empathy'
I believe the SELECT statement's syntax will be:
SELECT patronCode FROM tablename WHERE ....