I want to get the services that are filled and all the other services that are empty, but on excecute I get an message saying that
join contains a syntaxis error
SELECT OverzichtID, ServiceID, Service.Beschrijving, [Service Aantal]
FROM Service.Overzicht
LEFT JOIN Service
ON Service_Overzicht.ServiceID = Service.ServiceID
First, I think your table name in the FROM clause is wrong - it probably should be Service_Overzicht and not Service.Overzicht. Second, Access really likes parentheses - try ON (Service_Overzicht.ServiceID = Service.ServiceID) – Zohar Peled 6 mins ago
Related
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)
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'
Hello first time using Oracle SQL Developer and I'm trying to do a select join on two tables and I get the error "ORA-01722: invalid number". What is causing this error?
All of the Google results point to creates/inserts that are invalid.
Here is my query, both CUSTNO and ARCUSTO_ID are of type NUMBER(15,0)
SELECT
ARCUSTO.COMPANY, SHIP_TO.ADDR1
FROM
ARCUSTO
INNER JOIN
SHIP_TO ON ARCUSTO.CUSTNO = SHIP_TO.ARCUSTO_ID;
EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #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 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;
I know there is more than one question out there that matches this, but I am relatively new to mysql, and I can't seem to make this work using sub quests or the USING key word, plus I find the mysql on line docs a complete mystery.
I started trying to build my DELETE query using a SELECT query as my base and was able to get all the rows that I wanted to delete:
select *
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
and then just replaced select all with DELETE so:
delete
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
I gather from both the error message and from other similar posts that you can't use 'ON' to join tables in a delete query, you have to use USING or a sub query. The query I built with USING returns a really strange error, first the query:
DELETE
FROM writings_tags_link
USING writing_tags_link INNER JOIN writing_tags
WHERE writing_tags.id = writings_tags_link.tag_id
AND writing_tags.tag ='tag one'
error:
#1109 - Unknown table 'writings_tags_link' in MULTI DELETE
This table does exist, obviously, my original select query returned the desired results. Any help / explanation would be so very appreciated!
Please keep in mind, I'm only trying to delete the data in the linking table.
Your information is incorrect about requiring the use of the USING keyword in DELETE syntax when using JOINs - the documentation provides examples in the multi-delete section:
DELETE wtl
FROM WRITINGS_TAGS_LINK wtl
JOIN WRITING_TAGS wt ON wt.id = wtl.tag_id
WHERE wt.tag = 'tag one'