MySQL InnerJoin - Unknown column on clause - mysql

I've been messing about with MySQL for a while, got all my db setup and now I'm trying to join the tables together with InnerJoin, but I keep getting this "Unknown column on clause" error, tried googling but my code seems to be right.
Here's what the query looks like:
select
CharacterName, ClassName, PerkName
FROM
rpgcharacter
INNER JOIN
class ON rpgcharacter.idClass = class.idClass
inner join
perks on rpgcharacter.idRPGCharacter = perklist.idRPGCharacter;
Error
Code: 1054. Unknown column 'perklist.idRPGCharacter' in 'on clause'

Ahhh I see what I was doing wrong, apparently since I had not previously compared or even mentioned the query could not find the table, this is the solution I found.
select
rpgcharacter.CharacterName, class.ClassName,
perks.PerkName, skills.SkillName
from
perklist
inner join
rpgcharacter on perklist.idRPGCharacter = rpgcharacter.idRPGCharacter
inner join
class on rpgcharacter.idClass = class.idClass
inner join
perks on perklist.idPerks = perks.idPerks
inner join
skilllist
inner join
skills on skilllist.idSkill = skills.idSkills
and skilllist.idRPGCharacter = rpgcharacter.idRPGCharacter
where
perklist.idRPGCharacter = 3

Related

SQL - Passing column in subquery

my command:
Select sobe.Id, sobe.Naziv, sobe.Opis, sobe.Kat, (Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG) AS ImePrezime from Sobe
This gives me this error:
1054 - Unknown column 'sobe.Id' in 'on clause'
I see what the problem is but I cant figure out how to fix it.
I want to pass sobe.Id inside of this subquery:
(Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG)
I want to check which students are in that room.
Word soba means room and JMBAG is like personal number for each student
Try moving it to a WHERE clause:
Select s.Id, sobe.Naziv, s.Opis, s.Kat,
(Select Group_CONCAT(si.Ime, si.Prezime) as ImePrezime
from studentsoba ss LEFT JOIN
studenti si
on si.JMBAG = ss.JMBAG
where ss.Id_Sobe = s.Id
) AS ImePrezime
from Sobe s;
The correlated subquery should be fine. I suppose the correlation clause cannot be in the on clause -- I normally put it in the where clause anyway.
Also note that I introduced table aliases so the query is easier to write and to read.

how to delete from more than two (2) tables using mysql joins

I'm trying to run a multi delete on a my database but when it runs it runs with no errors but nothing was deleted? is it a case that an inner join will fail if even one of the table doesn't have anything to link to another and on?
using the query in an Express.js api I'm trying to finish us.
I usual test big queries like this in DBMS before i place them in my api.
below is the query:
DELETE task,issue,milestone,help_source,project_task,project_source,project_milestone,task_created_by,mileStone_created_by,issue_created_by,source_created_by,task_issue,milestone_task,project_creation,project
from project_creation
INNER JOIN project_milestone ON project_milestone.projectId = project_creation.projectId
INNER JOIN project_task ON project_task.projectId = project_creation.projectId
INNER JOIN project_source ON project_source.projectId = project_creation.projectId
INNER JOIN task_created_by ON task_created_by.taskId = project_task.taskId
INNER JOIN mileStone_created_by On mileStone_created_by.mileStoneId = project_milestone.mileStoneId
INNER JOIN source_created_by ON source_created_by.sourceId = project_source.sourceId
INNER JOIN task_issue ON task_issue.taskId = project_task.taskId
INNER JOIN issue_created_by ON issue_created_by.issueId = task_issue.issueId
INNER JOIN milestone_task On milestone_task.taskId = project_task.taskId
INNER JOIN task On task.taskId = project_task.taskId
INNER JOIN issue ON issue.issueId = task_issue.issueId
INNER JOIN milestone ON milestone.mileStoneId = project_milestone.mileStoneId
INNER JOIN help_source On help_source.sourceId = project_source.sourceId
INNER JOIN project ON project.projectId = project_creation.projectId
WHERE project.projectId = 59;
is there some error in my logic? and if yes what is it? or is it deleting without me knowing?
Questions you may ask:
Did i try searching stack overflow?
ANS: yes i did but can't seem to find an answer that fits this weird situation
so there is no error?
ANS: no there is no errors when i run the query
try to look on that topic :
How to Delete using INNER JOIN with SQL Server?
As far as I could see you need to specify aliases for each table.
So task, issue... are aliases but cannot see any of then after inner-join table name
(true, same names as tables but maybe alias are required)
Eg: INNER JOIN task task ON...
(now task is an alias, so you delete from table task)
Didn't check it, but if is to be work that should be the fix.

SQL select from unlerated tables as well as related tables

I'm getting an error (#1054 - Unknown column 'post.make_id' in 'on clause' ) with the following query :
SELECT `post`.*
FROM `post`,
`city` `postCity`
LEFT JOIN `make` ON `post`.`make_id` = `make`.`id`
If I remove city` `postCity, no error.
Is there any way for me to query the city table even though it's not related to the post table and at the same time do the left join with other tables ?
I think your problem is the mixing of commas with proper JOIN syntax.
Does this do what you want?
SELECT p.*
FROM post p CROSS JOIN
city c LEFT JOIN
make m
ON p.make_id = m.id;
I'm not sure what you are trying to accomplish. This doesn't look particularly useful, but it might fix your syntax error.

MySQL INNER JOIN not working

Im trying to write this statement for a game ladder I am making. All the inner joins worked until I added the team names into the equation.
SELECT tblMatch.AttackingTeam,
tblMatch.DefendingTeam,
tblTeam.TeamName As AttackingTeamName,
tblTeam.TeamName As DefendingTeamName,
tblGameMaps.MapName AS MapName,
tblGameTypes.TypeShort AS TypeName,
tblStyles.StyleShort AS StyleName,
tblMatch.AttackingScore,
tblMatch.DefendingScore
FROM tblMatch
INNER JOIN tblGameMaps
ON tblGameMaps.MapID = tblMatch.MapID
INNER JOIN tblGameTypes
ON tblGameTypes.TypeID = tblMatch.TypeID
INNER JOIN tblStyles
ON tblStyles.StyleID = tblMatch.StyleID
INNER JOIN tblTeam A
ON A.TeamID = tblMatch.AttackingTeam
INNER JOIN tblTeam B
ON B.TeamID = tblMatch.DefendingTeam
WHERE LadderID=$ladderID AND (DefendingTeam=$teamID OR AttackingTeam=$teamID)
The error I get is
Unknown column 'tblTeam.TeamName' in 'field list'
The database does have a table named tblTeam with a column tblTeamName. Maybe I just cant see the error???
You have already aliased tblTeam to A and B, so instead of using tblTeam.TeamName use either A.TeamName or B.TeamName as per your requirement.
You have a column tblTeamName in table tblTeam and in SELECT you are calling TeamName
tblTeam.TeamName As AttackingTeamName,
tblTeam.TeamName As DefendingTeamName,
And what is need to give alias twice to the same column
So why write the line
tblTeam.TeamName As AttackingTeamName,
perhaps that is the problem (CTRL-F then enter "tblTeam.TeamName As AttackingTeamName,")

MySQL: "Unknown column in where clause" during Update Statement

UPDATE Recipes RE, (
SELECT SUM((((i.iCaseCost/i.iCaseQty)/i.iUnitSize)/i.iUnitSoldBy)*ri.riQty*ri.riMeasureBy) AS 'RecipeCost'
FROM Recipes r INNER JOIN RecipeIngredients ri
ON r.rID = ri.rID JOIN Ingredients i
ON ri.iID = i.iID
WHERE ri.rID = RE.rID
) t
SET RE.rYieldCost = t.RecipeCost
When executed, I get the following error: "Unknown column 'RE.rID' in 'where clause'".
Any ideas?
Your inner derived query doesn't know anything about columns in the outer query. Try moving the WHERE into the UPDATE clause with something like WHERE t.rID = RE.rID.