I've been working on a SQL query for a project, and I face an error message when I want to use it.
Here is the query itself :
SELECT COUNT(r) AS auditMade,
SUM(g.nbrMilkingCows) AS cowsAudited,
AVG(r.gainPerCowPerYearTransition) AS averageGainTransition,
AVG(r.gainPerCowPerYearLactation) AS averageGainLactation,
AVG(r.totalGain) AS averageTotalGain,
AVG(r.supplementalCostPerCow) AS averageSuppCost
FROM `smart_calculator_infos` i
INNER JOIN `smart_calculator_result` r ON r.idSmartCalculatorResult = i.idSmartCalculatorResult
INNER JOIN `calculator_general_informations` g ON g.idSmartCalculatorInfo = i.idSmartCalculatorInfo
WHERE i.idUser = 14
MySQL answers me "Unknown column 'r' in field list".
But I dont really understand why I get an error here as I define r in my INNER JOIN.
I'm kinda new at using SQL so maybe there is something pretty obvious I forgot, but I can't seem to understand what.
You can't count an alias itself, so the very first line of your query is what is causing the error:
SELECT COUNT(r)
To remedy this, you could use COUNT(*):
SELECT COUNT(*)
Or, you could count an actual column in the smart_calculator_result table, e.g.
SELECT COUNT(r.idSmartCalculatorResult)
Related
I'm trying to build an SQL-Query with MySQL-Version 5.7.27.
I'm trying to make multiple left joins to the same table. At some point i get an error, that tells me, a column of the original table is unknown.
I tried finding the syntax error in my query, but there can't seem to be an obvious one. I tried out some online SQL-validators. They keep telling me, my query is valid.
After that i tried picking apart the query, to find out, what exactly i am working with before the left join that makes my query unable to function. Doing that actually showed me a result, that was including the column the error was telling me, that is missing.
So maybe it is not a syntax error, but MySQL is validating things in a order different to what i know, trying to pick the "missing" column out of somewhere it actually doesn't exist.
Changing the order of left joins actually solves the problem. I can't really do that, tho. The SQL is being generated by some functions. One for every part of the SQL. One for the "SELECT", one for the "FROM" and so on. So a working thing is joining the minimized versions of the "additional_agreement_fields" table before the other joins. Sadly the function for the "FROM" part is written ... in a very interesting way. So my best bet is adding the "additional_agreement_fields" joins after the function that creates the FROM.
That attempt generated the SQL below.
I get following error:
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
So:
- The parts of the query are working on their own
- Execution until the point of the error holds a table that has the
"missing column"
- Changing the order of the joins solves the problem, but i can't just
do that and i want to know, why it doesn't work in this order
- Online-validators can't find any syntax errors
- Logically i am just adding columns to a table that are not ambiguous
and not removing any. Yet something becomes unable to be found
SELECT DISTINCT
(customers.id),
companies.company_name AS 'Kunde -> Firmenname',
vcpt_mail.tcom_value AS 'Kontakt -> TCom -> Email',
v_contact_people.id AS 'Kontakt -> ID',
v_contact_people.last_name AS 'Kontakt -> Nachname',
A0.value AS 'Kunde -> Zusatzvereinbarung -> TestfeldB'
FROM
customers
LEFT JOIN
customer_types ON customer_types.id =
customers.customer_type_id,
v_customer_owners,
companies
LEFT JOIN
(v_contact_people
LEFT JOIN v_cp_tcoms AS vcpt_mail ON
vcpt_mail.contact_person_id = v_contact_people.id
AND vcpt_mail.ttid = 3
AND (vcpt_mail.type_label_access_path = '/Global'
OR vcpt_mail.type_label_access_path LIKE '/Global%')) ON
v_contact_people.company_id = companies.id
AND v_contact_people.access_path LIKE '/Global%'
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldB' AND value = '5') AS A0 ON customers.id
= A0.customer_id
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldC' AND value = '6') AS A1 ON customers.id
= A1.customer_id
WHERE
v_customer_owners.customer_id = customers.id
AND v_customer_owners.path LIKE '/Global%'
AND customers.company_id = companies.id
AND companies.deleted_at IS NULL
AND customers.deleted_at IS NULL
AND (A0.value = '5' AND A0.name = 'TestfeldB'
OR A1.value = '6' AND A1.name = 'TestfeldC');
The expected result is a not empty table with 2 customers in it, i prepared to find and that are found, changing the join order.
The result is an interrupted query with an error code.
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
I'm trying to run this query:
SELECT qry_performPrep2Elo_wta.PK_G, IIf([ID1_ocEdge]>0,[ID1_ocStake],0) AS ocStake, IIf([ID1_ocEdge]>0,[ID1_ocStake]*([ID1_ocmktPr]-1)) AS ocPL
FROM tbl_G_ov_wta INNER JOIN qry_performPrep2Elo_wta ON tbl_G_ov_wta.PK_G = qry_performPrep2Elo_wta.PK_G
WHERE (((tbl_G_ov_wta.ID1_G_Tot_Ov)>=(SELECT Min_games_wta FROM [tbl_ref_games] WHERE Surface = "All surfaces")));
But I get an "Invalid argument to function" error. If I remove the WHERE clause and run the following:
SELECT qry_performPrep2Elo_wta.PK_G, IIf([ID1_ocEdge]>0,[ID1_ocStake],0) AS ocStake, IIf([ID1_ocEdge]>0,[ID1_ocStake]*([ID1_ocmktPr]-1)) AS ocPL
FROM tbl_G_ov_wta INNER JOIN qry_performPrep2Elo_wta ON tbl_G_ov_wta.PK_G = qry_performPrep2Elo_wta.PK_G;
Everything works fine. I investigated the WHERE clause and couldn't see a reason for an issue. Indeed, when I remove the IIF statements but include the WHERE clause:
SELECT qry_performPrep2Elo_wta.PK_G
FROM tbl_G_ov_wta INNER JOIN qry_performPrep2Elo_wta ON tbl_G_ov_wta.PK_G = qry_performPrep2Elo_wta.PK_G
WHERE (((tbl_G_ov_wta.ID1_G_Tot_Ov)>=(SELECT Min_games_wta FROM [tbl_ref_games] WHERE Surface = "All surfaces")));
Everything runs fine.
The field PK_G is a primary key common across both the table and the query.
Any ideas?
You missed the zero here:
IIf([ID1_ocEdge]>0,[ID1_ocStake]*([ID1_ocmktPr]-1),0)
I'm getting this MySQL Inner Join query error and, although I have tried very hard to figure out, I can't figure out where the bug is.
SELECT db_trailer_id.*, db_trailer_locs.*
FROM db_trailer_id INNER JOIN db_trailer_locs
ON db_trailer_id.id = db_trailer_locs.trailer_id
WHERE db_trailer_id.vin = XXXXXX
Tables and field names have been checked and double checked.
db_trailer_id
id
vin
db_trailer_locs
id
trailer_id
location
Is there something missing in the syntax? Is it because I'm trying to match to a column in the first (parent) table?
Any help would be appreciated.
You should quote your values, else it will be considered as columns:
SELECT `db_trailer_id`.*, `db_trailer_locs`.*
FROM `db_trailer_id` INNER JOIN `db_trailer_locs`
ON `db_trailer_id`.`id` = `db_trailer_locs`.`trailer_id`
WHERE `db_trailer_id`.`vin` = 'XXXXXX'
Would recommend adding backticks, but totally your wish.
I have already read a majority of this similarly asked question but due to the specific nature of most of the answers, none helped, or I was unable to apply the answer.
I need to convert this SELECT statement into an UPDATE statement, the SELECT statement works just fine.
SELECT sf_state_filings_cstm.recurrency_c, sf_state_filings_cstm.date_filed_c,
sf_state_filings_cstm.status_c
FROM sc_state_configuration_cstm
INNER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
INNER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
WHERE sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
This is what I have so far after working through the following errors:
When I first wrote the update statement, I got:
MySQL code error 1066: Not unique table/alias: ‘sf_state_filings_cstm
Solution: Why does this SQL code give error 1066 (Not unique table/alias: 'user')?
Then I got this error:
1052: Column ‘recurrency_c' in field list is ambiguous
Solution: 1052: Column 'id' in field list is ambiguous
Now I have this error:
Error : Unknown column 'sc_state_configuration_cstm.id_c' in 'where
clause'
None of the below links have helped so far, or I was doing something wrong
Unknown Column In Where Clause
Unknown Column in Where Clause
MySQL: "Unknown column in where clause" during Update Statement
I have a feeling the answer has to do with using an alias as mentioned in one the links, using HAVING instead of WHERE, but just replacing WHERE with HAVING doesn't seem to work.
Here is my syntax right now:
UPDATE `my_database`.`sf_state_filings_cstm`
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c AS joined_tables ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm AS main_table ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET main_table.recurrency_c = 'Perpetual',
main_table.expiration_date_c = ''
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
EDIT 1:
Here is how the Tables have to be linked to each other:
I also realized, I need to be doing a LEFT OUTER JOIN instead of an INNER JOIN. I have updated the above syntax. The middle table stores the id's from both tables. That's how the relationship is stored. If more information is needed, let me know.
[SOLUTION]
UPDATE sc_state_configuration_cstm
LEFT OUTER JOIN sc_state_configuration_sf_state_filings_1_c ON sc_state_configuration_cstm.id_c = sc_state_configuration_sf_state_filings_1_c.sc_state_c2445uration_ida
LEFT OUTER JOIN sf_state_filings_cstm ON sc_state_configuration_sf_state_filings_1_c.sc_state_configuration_sf_state_filings_1sf_state_filings_idb = sf_state_filings_cstm.id_c
SET sf_state_filings_cstm.recurrency_c = 'Perpetual',
sf_state_filings_cstm.expiration_date_c = null
WHERE
sc_state_configuration_cstm.id_c = '2d9b438e-01e1-ccb2-82e5-5721221114bb'
Thanks to jyncka and a lot of other posts I read, where i noticed that when converting a SELECT to an UPDATE, they simply wrote UPDATE (table from the 'FROM' statement).
I went back and noticed I had written:
FROM sc_state_configuration_cstm
The error is telling you what's happening: id_c is not a column that exists in the sc_state_configuration_cstm table. If you do a DESCRIBE on sc_state_configuration_cstm it will show you the correct field name and you can drop that in instead of id_c.
Or you might have used the wrong alias in your WHERE statement. Without knowing what your tables look like, it's difficult to say which is the case.
Edit: I believe I see the problem. Here are the tables you are explicitly using in the statement:
sc_state_configuration_sf_state_filings_1_c
sf_state_filings_cstm
But you are using this table in the WHERE clause:
sc_state_configuration_cstm
You need to join sc_state_configuration_cstm so that it can be used. It took me a minute to see it because sc_state_configuration_cstm and sf_state_filings_cstm look similar at first glance.
I'm trying to join some tables together in MySQL, but I seem to get an error saying: #1066 - Not unique table/alias: 'calendar_jobs'
I really want it to select everything from the cal_events, the 2 user bits and just the destination col from the jobs table, but become "null" if there arn't any job. A right join seemed to fit the bill but doesn't work! Can anyone help!?
UPDATE:
Thanks for the help on the previous query, I'm now up to this:
SELECT calendar_events.* , calendar_users.doctorOrNurse, calendar_users.passportName, calendar_jobs.destination
FROM `calendar_events` , `calendar_users`
RIGHT JOIN calendar_jobs ON calendar_events.jobID = calendar_jobs.jobID
WHERE `start` >= 0
AND calendar_users.userID = calendar_events.userID;
But am now getting an error saying: #1054 - Unknown column 'calendar_events.jobID' in 'on clause'
What is it this time!?
Thanks again!
You shouldn't use calendar_jobs in the FROM clause, because you've already specified it in the JOIN. Try this:
SELECT calendar_events.* , calendar_users.doctorOrNurse, calendar_users.passportName, calendar_jobs.destination
FROM `calendar_events` , `calendar_users`
RIGHT JOIN calendar_jobs ON calendar_events.jobID = calendar_jobs.jobID
WHERE `start` >=0
AND calendar_users.userID = calendar_events.userID
Answer for update:
All evidence seems to indicate that the column doesn't exist in that table :).
Try this:
SELECT calendar_events.* , calendar_users.doctorOrNurse, calendar_users.passportName, calendar_jobs.destination
FROM `calendar_users`, `calendar_events`
RIGHT JOIN calendar_jobs ON calendar_events.jobID = calendar_jobs.jobID
WHERE `start` >=0
AND calendar_users.userID = calendar_events.userID
The order of the tables in the FROM has been switched, because you join events with jobs.
Run this: show create table calendar_events; , and post the results here.
We need to see the table structure to answer your second question.
You have calendar_jobs listed twice in your query.
Once on the second line and once on the RIGHT JOIN statement.