SQL select from unlerated tables as well as related tables - mysql

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.

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 ;

MySQL InnerJoin - Unknown column on clause

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

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.

Inner join not works

After a long work on it i am here going to ask my question, its simple but i don't know why it is not working, please help!!!
I have a table
phpfox_friend (where i have the below columns)
friend_id
user_id
friend_user_id
ordering
phpfox_user (where i have below columns)
usre_id
user_name
status_id
full_name
I am trying to inner join it by using below mentioned Sql query, but it give me an error
#1054 - Unknown column 'phpfox_friend.user_id' in 'on clause'
Query
SELECT *
FROM `phpfox_friend`
INNER JOIN `phpfox_user`
ON `phpfox_friend.user_id`=`phpfox_user.user_id`
WHERE phpfox_user.user_name IS NOT NULL
Kindly guide me what i am doing wrong in it
try this:
SELECT * FROM `phpfox_friend`
INNER JOIN `phpfox_user` ON
`phpfox_friend`.`user_id`=`phpfox_user`.`user_id`
WHERE `phpfox_user`.`user_name` IS NOT NULL
you need to add the quotes to tables and to the fields.
so use
`phpfox_friend`.`user_id` instead of `phpfox_friend.user_id`