MySQL INNER JOIN not working - mysql

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,")

Related

MySQL - Subquery in SELECT raising "unkown colum" on outer reference

I am trying to get a field which is calculated by a subquery. I found about 400 posts at SO that state that you can use the outer ID in a select subquery clause unless you are trying to use it in a join (what I don't).
Here's my query:
SELECT (SELECT group_concat(ct.NAME)
FROM core_tagobject cto
JOIN core_tag ct ON ct.id=cto.tag_id
AND cto.context="tags_working_on"
AND cto.object_id=u.id) AS "tag_list"
FROM auth_user u
I always get back SQL error (1054) - unknown column 'u.id' in 'on clause'.
What am I doing wrong?
Thx!
I guess you want below sql
SELECT group_concat(ct.NAME) AS `tag_list`
FROM core_tagobject cto JOIN core_tag ct ON ct.id=cto.tag_id
JOIN auth_user u ON cto.object_id=u.id
WHERE cto.context="tags_working_on"
With some data examples would be better to produce a proper solution, but could you try:
SELECT tag_list.*
FROM auth_user u
INNER JOIN (
SELECT group_concat(ct.NAME)
FROM core_tagobject cto
JOIN core_tag ct ON ct.id=cto.tag_id
AND cto.context="tags_working_on"
) AS tag_list on tag_list.object_id=u.id ;

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 use "USING" keyword to join three table and display it using mysql syntax

this is how i manage to join the table using inner join
SELECT lab5enrollment.matricno, lab5student.stuname,
lab5enrollment.courseid,
lab5course.cname
FROM ((lab5enrollment
INNER JOIN lab5student ON lab5enrollment.matricno = lab5student.matricno)
INNER JOIN lab5course ON lab5enrollment.courseid = lab5course.courseid)
WHERE lab5enrollment.courseid = 'CSF3402';
this is how i used the using keyword to join the table but i dont know how to join the three table...
SELECT matricno, stuname, courseid, cname
FROM lab5enrollment
JOIN lab5student
USING (matricno)
WHERE courseid = 'CSF3402';
i want to observe the differrence between using the inner join and using...
You should probably lean towards using joins with explicit ON clauses for a number of reasons. If you wanted to use USING here, then the following should work:
SELECT
t1.matricno,
t2.stuname,
t1.courseid,
t3.cname
FROM lab5enrollment t1
INNER JOIN lab5student t2
USING (matricno)
INNER JOIN lab5course t3
USING (courseid)
WHERE
t1.courseid = 'CSF3402';
This assumes that lab5enrollment and lab5student both have a column with the same name matricno, and that lab5student and lab5course both have a column called courseid.

SQL Join Issues Microsoft Query

I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name

subquery is not working in mysql

This is my mysql query
SELECT tm.MAGAZINE_ID, tm.MAGAZINE_NAME,tm.MAGAZINE_DESCRIPTION,pub.publisher_name,
tmi.COVER_PAGE_THUMB AS COVER_PAGE_VERTICAL,tmi.FROM_DATE AS ISSUE_DATE,
tm.html_flag AS HTML_EXIST,tm.CATEGORY_ID,tm.language_id,tm.is_free,tma.AppUrl,
(SELECT issue_id from tbl_magazine_issue WHERE magazine_id = 141
ORDER BY FROM_DATE DESC LIMIT 1) as temp_issue_id
FROM tbl_magazine_apps as tma
LEFT OUTER JOIN tbl_magazine_code as tmc ON tmc.Code = tma.AppsCode
LEFT OUTER JOIN `tbl_magazine` AS tm ON tmc.magazine_Id = tm.MAGAZINE_ID
JOIN `tbl_magazine_issue` AS tmi ON temp_issue_id = tmi.issue_id
LEFT OUTER JOIN mst_publisher AS pub ON tm.publisher_id=pub.publisher_id
WHERE
tmi.PUBLISH_STATUS IN(1,3)
AND tmi.`OS_SELECT` = '".$osType."'
AND tma.id IN (".$appIds.")
GROUP BY tm.MAGAZINE_ID
ORDER BY tmi.ISSUE_DATE DESC
but i got an error that
#1054 - Unknown column 'temp_issue_id' in 'on clause'
if any one know about this please help me. i am new to this
AFAIK The subquery belongs to the from part:
http://dev.mysql.com/doc/refman/5.7/en/from-clause-subqueries.html
So I would join the subquery.
Like:
SELECT a.a, b.b
FROM table1 as a
JOIN (SELECT b from table2) as b ON a.key = b.key;
As the message suggests, the column temp_issue_id is not in any of the following tables: tbl_magazine_apps, tbl_magazine, or tbl_magazine_issue. It is also not a variable in the environment.
Beyond that, it is pretty much impossible for anyone to figure out how to fix the problem without more knowledge about the data structure.
If I were to hazard a guess, based on the table names, that particular join would be:
JOIN `tbl_magazine_issue` AS tmi ON tm.magazine_id = tmi.magazine_id
because it makes sense to me that a magazine issue would be connect to a magazine. I have no idea what temp_issue_id is, though.
temp_issue_id give alias name for specifying this column i think it should be tm.issue_id
temp_issue_id is column's name as per the query. You need it to convert to table and use subsequent column in your SELECT clause.