The following query generates the error
Unknown column 'a.household_id' in 'on clause'
in phpmyadmin but not in MySQL Workbench. phpmyadmin (3.5.3) is looking at a remote server running MySQL server 5.5.27, Workbench 6.1.4 is looking at localhost running 5.5.37. [I have reproduced the unknown column error described in the MySQL documentation in Workbench so the docs provide no immediate direction for eliminating this error.]
SET #end_year = (select if(month(now()) < 7, year(now()) -1, year(now())));
SET #start_year = #end_year - 4;
SELECT if(MONTH(contact_date) >= 7, CONCAT('FY ', YEAR(contact_date) + 1), CONCAT('FY ', YEAR(contact_date))) AS FY,
FORMAT(SUM(A.size), 0) AS DI, FORMAT(COUNT(c.household_id), 0) AS DH
FROM contact c
JOIN
(
SELECT m.household_id, if(COUNT(dob) = 0, 1, COUNT(dob)) size
FROM member m
GROUP BY m.household_id
) A ON a.household_id = c.household_id
WHERE contact_date BETWEEN CONCAT(#start_YEAR - 1, '-07-01') AND CONCAT(#end_YEAR, '-06-30')
GROUP BY if(MONTH(contact_date) >= 7, YEAR(contact_date) + 1, YEAR(contact_date));
Your table alias is A not a and I guess it matters in your case. Try the following :
A ON A.household_id = c.household_id
Related
I recently update mysql to 8.0. Since this update a syntax error is reported in this query:
UPDATE
MinMaxAvg AS tar
INNER JOIN (
SELECT
SUBSTR(YYYYDD, 1, 8) AS timestamp,
ROUND(AVG(rainfall), 1) AS rain_total_avg,
ROUND(MIN(rainfall), 1) AS rain_total_min,
ROUND(MAX(rainfall), 1) AS rain_total_max
FROM
(
(
SELECT
MAX(rain_total) - MIN(rain_total) AS rainfall,
substr(timestamp, 1, 8) AS YYYYDD
FROM
weather
WHERE
timestamp > 0
GROUP BY
substr(timestamp, 1, 8)
) AS T1
)
GROUP BY
SUBSTR(YYYYDD, 1, 8)
) AS sor ON tar.timestamp = sor.timestamp
SET
tar.rain_total_avg = sor.rain_total_avg,
tar.rain_total_min = sor.rain_total_min,
tar.rain_total_max = sor.rain_total_max
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 ') GROUP BY SUBSTR(YYYYDD,1, 8)) AS sor ON tar.timestamp = sor.timestamp SET tar.
I already checked the query with several online check tools. All reported the code as okay.
What is wrong with the query?
I already checked the query with several online check tools. All reported the code as okay.
I had to remove the parantheses around T1 block:
UPDATE
MinMaxAvg AS tar
INNER JOIN (
SELECT
SUBSTR(YYYYDD, 1, 8) AS timestamp,
ROUND(AVG(rainfall), 1) AS rain_total_avg,
ROUND(MIN(rainfall), 1) AS rain_total_min,
ROUND(MAX(rainfall), 1) AS rain_total_max
FROM
(
SELECT
MAX(rain_total) - MIN(rain_total) AS rainfall,
substr(timestamp, 1, 8) AS YYYYDD
FROM
weather
WHERE
timestamp > 0
GROUP BY
substr(timestamp, 1, 8)
) AS T1
GROUP BY
SUBSTR(YYYYDD, 1, 8)
) AS sor ON tar.timestamp = sor.timestamp
SET
tar.rain_total_avg = sor.rain_total_avg,
tar.rain_total_min = sor.rain_total_min,
tar.rain_total_max = sor.rain_total_max
I found out that the windows based sql client "HeidiSQL" might be used to find syntax errors in queries.
I'm using NodeJS v8.7.0 with the promise-mysql module, alongside mysql v14.14 Distrib 5.5.57.
I um unable to insert into the temporary table I create. On online SQL validators, as well as my project, I receive the error around the INSERT command.
Whilst I can personally see no issues, I'd love to see a fresh perspective of things to spot the issue.
My whole query string:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;DROP TEMPORARY TABLE QueryBuilder;
Query line-by-line:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;
INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);
UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;
DROP TEMPORARY TABLE QueryBuilder;
Extra info:
'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 \'INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1,\' at line 1'
errno: 1064
An SQLFiddle demonstrating why I made this query
Thanks for reading my issue, I appreciate all feedback!
I am trying a simple query in mysql and I am getting a syntax error that I need help understanding.
SELECT
eea.*,
ee.description,
eect.title,
eect.file,
eect.location,
eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1,
eea.ee_id = ee.id,
ee.eect_id = eect.id;
I am getting the following error:
Basically the syntax error on line 13
eea.ee_id = ee.id, ee.eect_id = eect.id LIMIT 0, 25
Anyone have idea how I can edit this to get the -1 vote to improve?
Its a simple syntax error, your WHERE clause should not be seperated by commas. Use AND or OR etc
SELECT
eea.*, ee.description, eect.title, eect.file,
eect.location,eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1 AND
eea.ee_id = ee.id AND
ee.eect_id = eect.id
LIMIT 0,25
You should also learn about the JOIN syntax
SELECT
eea.*, ee.description, eect.title,eect.file,
eect.location, eect.img_location
FROM `e_exam_attempt` eea
JOIN `e_exam` ee ON eea.ee_id = ee.id
JOIN `e_exam_cert_template` eect ON ee.eect_id = eect.id
WHERE
eea.a_user_id = 1
LIMIT 0,25
Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#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 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group
I was rolling some queries into a stored procedure and I hit the #1327 - Undeclared variable error ... the odd thing though is that the variable it claims is undeclared is actually a table name.
Working through the problem
So I extracted the bit of the procedure where it was falling over and tried to run it as a normal SQL query directly on the database through PHPMyAdmin... same thing. After much tinkering it seems to be where I'm joining another table.
If I run the query on a single table, it's fine, like this:
SET #i_channel_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code
FROM `product_status_to_channel`
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC,
`product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code;
Which outputs 'LIVE' as the status_code in PHPMyAdmin - which is fine.
However, when I try and JOIN to the message table to find the associated status message, I get the error: #1327 - Undeclared variable: product_status_to_channel_lang ... but product_status_to_channel_lang is a table?!
SET #i_channel_id = 3;
SET #i_language_id = 3;
SET #i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
FROM `product_status_to_channel`
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
AND `product_status_to_channel_lang`.`language_id` = #i_language_id
WHERE `product_status_to_channel`.`channel_id` = #i_channel_id
AND `product_status_to_channel`.`product_id` = #i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT #s_status_code AS status_code, #s_status_message AS status_message;
Is it trying to evaluate product_status_to_channel_lang.product_status_to_channel_id as a variable on the JOIN?
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
I assume I'm overlooking something obvious?
I've tried this on both:
a Win7 box running xampp with MySQL 5.5.27 - MySQL Community Server (GPL)
a Debian box running MySQL 5.1.73-1-log - (Debian)
Never mind - it was something obvious:
SELECT
`product_status_to_channel`.`status_code` INTO #s_status_code,
`product_status_to_channel_lang`.`string` INTO #s_status_message
Should be:
SELECT
`product_status_to_channel`.`status_code`,
`product_status_to_channel_lang`.`string`
INTO
#s_status_code,
#s_status_message
... it must be Friday, it took literally a couple of hours to see that.
... INTO #s_status_code,
`product_status_to_channel_lang`.`string` ...
^That's where it's trying to assign product_status_to_channel_lang to being a variable into which to put data.