Unknown Column in On Clause (but does exist) - mysql

I've had a look at some other questions like this but I'm not sure I understand/can apply it here. I know my DB has this column for t and for w as well but it doesn't like either. Can someone please take a look and see if they can figure out the issue? :)
ERROR: Unknown column 't.id_cat' in 'on clause'
SELECT
c.id_cat,
c.cat_name,
t.id_type,
t.type_name,
t.type_desc,
t.num_works,
t.num_comments,
w.id_work,
t.child_level,
w.id_member,
mg.group_name,
m.real_name,
w.work_title,
w.work_cap,
u.filetype,
u.location,
w.id_feedback,
w.id_series,
w.id_triggers,
w.is_adult,
w.poster_time,
w.work_comments,
w.work_views
FROM
smf_works_uploads as u
LEFT JOIN
smf_works_works AS w ON (w.id_work = u.id_work)
LEFT JOIN
smf_members AS m ON (m.id_member = w.id_member)
LEFT JOIN
smf_membergroups AS mg ON (mg.id_group = m.id_group)
LEFT JOIN
smf_works_categories AS c ON (c.id_cat = t.id_cat)
LEFT JOIN
smf_works_types AS t ON (t.id_type = w.id_type)
WHERE
t.id_type = 16

Switch the two joins
LEFT JOIN
smf_works_types AS t ON (t.id_type = w.id_type)
LEFT JOIN
smf_works_categories AS c ON (c.id_cat = t.id_cat)
Because you can only use t if you already joined the table before.

Related

How to fix "1054 - unknown column on clause"

I wrote a sql code to get the data from my MySQL database. but when i run sql query it returns an error.
How an I fix this. Im new to this and I googled but i cant find a solution.
1054 - Unknown column 'departments.department_id' in 'on clause'
SELECT
staff_data.first_name,
staff_data.middle_name,
staff_data.last_name,
staff_data.gender,
staff_data.civil_status,
staff_data.birthday,
staff_data.address_line_1,
staff_data.address_line_2,
staff_data.address_line_3,
staff_data.contact_no_1,
staff_data.contact_no_2,
staff_data.nationality,
staff_data.religion,
staff_data.email,
staff_data.is_deleted,
staff_data.nic_number,
staff_data.is_admin,
departments.department_name,
job_positions.position_name
FROM
departments,
employee_position
INNER JOIN job_positions ON job_positions.department_id =
departments.department_id
AND departments.department_id = job_positions.department_id
AND employee_position.position_id = job_positions.position_id
INNER JOIN staff_data ON employee_position.user_id =
staff_data.nic_number
WHERE
employee_position.is_valid = 1
Please don't mix old school join syntax with modern syntax (the latter which you should be using only). Here is your corrected query using modern join syntax:
SELECT
s.first_name,
s.middle_name,
s.last_name,
s.gender,
s.civil_status,
s.birthday,
s.address_line_1,
s.address_line_2,
s.address_line_3,
s.contact_no_1,
s.contact_no_2,
s.nationality,
s.religion,
s.email,
s.is_deleted,
s.nic_number,
s.is_admin,
d.department_name,
j.position_name
FROM departments d
INNER JOIN job_positions j
ON j.department_id = d.department_id
INNER JOIN employee_position e
ON e.position_id = j.position_id
INNER JOIN staff_data s
ON e.user_id = s.nic_number
WHERE
e.is_valid = 1;
The error you are seeing is probably coming from the fact that your use of old school join syntax put the MySQL parser into a certain mode, which is then not behaving as you expect subsequently.
Note that I also introduced table aliases, which makes the query easier to read.
Issue is with first join
SELECT
staff_data.first_name,
staff_data.middle_name,
staff_data.last_name,
staff_data.gender,
staff_data.civil_status,
staff_data.birthday,
staff_data.address_line_1,
staff_data.address_line_2,
staff_data.address_line_3,
staff_data.contact_no_1,
staff_data.contact_no_2,
staff_data.nationality,
staff_data.religion,
staff_data.email,
staff_data.is_deleted,
staff_data.nic_number,
staff_data.is_admin,
departments.department_name,
job_positions.position_name
FROM
departments,
employee_position
INNER JOIN job_positions ON
job_positions.department_id = <change to req col>
-- departments.department_id join between employee_position and job_positions, but join is on department table
AND departments.department_id = job_positions.department_id
AND employee_position.position_id = job_positions.position_id
INNER JOIN staff_data ON employee_position.user_id =
staff_data.nic_number
WHERE
employee_position.is_valid = 1

MySQL Select and Join - Ambiguous column

I'm trying to join four tables together. The code works fine when I'm only comparing the EVENT_DATA and joining the PERSONA tables as I'm able to get the "Name" column from PERSONA (which doesn't exist in the EVENT_DATA table). However, one of the problems is that this "Name" column also exists in the CUSTOMCAR table, so I can only get one or the other. Additionally, when I tried adding the last join statement, the code wouldn't run at all.
If someone could please help me, that would be great!
$sql = "SELECT * FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299')";
You should avoid * and use explicit columns list instead:
SELECT EVENT_DATA.personaId, ...
FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299');
If you have same column name you need to to use aliasing:
SELECT ...
CUSTOMCAR.NAME AS c_name,
PERSONA.NAME AS p_name
...
You could also use Aliasing:
$sql = "SELECT ed.*,
pa.*,
cc.*,
ccs.*
FROM EVENT_DATA AS ed
LEFT JOIN PERSONA AS pa ON ed.personaId = pa.ID
LEFT JOIN CUSTOMCAR AS cc ON cc.ownedCarId = e.carId
LEFT JOIN CARCLASSES AS ccs ON ccs.store_name = cc.name
WHERE (ed.EVENTID = '299')";
Note: Although as suggested by #Lukasz, you should really avoid using wildcard (*), and provide an explicit list of columns in the SELECT clause.

Mysql view are very slow

My mysql view is really slow in thousands of data how can we improve this functionality?
While fetch this view in 10000s data then it takes more than 30 sec. how could we revise this view table?
SELECT
i.jo_in_id,
j.*,
i.jo_in_week_number,
i.jo_in_client_ref_no,
i.cl_id AS jo_in_cl_id,
c.cl_short_name,
c.cl_business_name,
m.me_first_name,
m.me_last_name,
m.me_mobile,
sk.sk_name,
sk.sk_ticketed,
ti.ti_id,
ta.ta_name,
u.un_id,
u.un_from,
u.un_to,
v.ve_name,
mp.vmp_name,
r.vr_name
FROM
jo_2_no j2n,
jo_in_numbers i,
jobs j
LEFT JOIN venues_new v ON j.ve_id = v.ve_id
LEFT JOIN venues_meeting_place mp ON j.vmp_id = mp.vmp_id
LEFT JOIN venues_rooms r ON j.vr_id = r.vr_id
LEFT JOIN clients c ON j.cl_id = c.cl_id
LEFT JOIN members m ON j.me_id = m.me_id
LEFT JOIN skills sk ON j.sk_id = sk.sk_id
LEFT JOIN tasks ta ON j.ta_id = ta.ta_id
LEFT JOIN crew_tickets ti ON j.sk_id = ti.sk_id AND j.me_id = ti.me_id AND j.jo_time_off < ti.ti_expire
LEFT JOIN unavailability u ON j.me_id = u.me_id AND ((j.jo_time_on BETWEEN u.un_from AND u.un_to) OR (j.jo_time_on BETWEEN u.un_from AND u.un_to))
WHERE
j.jo_id = j2n.jo_id
AND j2n.jo_in_numbers_id = i.jo_in_id
AFTER user EXPLAIN SELECT following is the output
In your EXPLAIN, I see that your joined tables ti and u are read with table-scans (type: ALL). This is probably the biggest problem for your performance.
You should make sure you have the following indexes created:
ALTER TABLE crew_tickets ADD KEY (sk_id, me_id, ti_expire);
ALTER TABLE unavailability ADD KEY (me_id, un_from, un_to);
That should help the joins to those tables work with index lookups instead of table-scans. I think they'll be accessed as covering indexes, too.
Also, please don't use the outdated "comma-joins." Especially do not mix both styles. It will bite you when you get surprised by the order of precedence between comma-joins and JOIN operators. See examples in Can someone help explain why not using a SQL JOIN is bad practice and wrong? or Error on JOIN mysql.
Write your joins this way:
FROM jo_2_no j2n
INNER JOIN jo_in_numbers i ON j2n.jo_in_numbers_id = i.jo_in_id
INNER JOIN jobs j ON j.jo_id = j2n.jo_id
LEFT JOIN venues_new v ON j.ve_id = v.ve_id
LEFT JOIN venues_meeting_place mp ON j.vmp_id = mp.vmp_id
LEFT JOIN venues_rooms r ON j.vr_id = r.vr_id
LEFT JOIN clients c ON j.cl_id = c.cl_id
LEFT JOIN members m ON j.me_id = m.me_id
LEFT JOIN skills sk ON j.sk_id = sk.sk_id
LEFT JOIN tasks ta ON j.ta_id = ta.ta_id
LEFT JOIN crew_tickets ti ON j.sk_id = ti.sk_id
AND j.me_id = ti.me_id AND j.jo_time_off < ti.ti_expire
LEFT JOIN unavailability u ON j.me_id = u.me_id
AND j.jo_time_on BETWEEN u.un_from AND u.un_to
I removed the redundant term in the join condition for u. The optimizer might eliminate that logic, but why make it work so hard?

MYSQL ERROR: unknown table `airports`

I am trying to run the following query:
SELECT `aalv_test`.`aircraft`.*, `aalv_test`.`airports`.*, `aalv_test`.`bids`.*
FROM `bids`
LEFT JOIN `aalv_test`.`pilots` ON `bids`.`pid` = `pilots`.`id`
LEFT JOIN `aalv_test`.`schedules` ON `bids`.`fid` = `schedules`.`id`
LEFT JOIN `aalv_test`.`aircraft` ON `schedules`.`aircraft` = `aircraft`.`id`
LEFT JOIN `aalv_test`.`airports` AS `arr` ON `schedules`.`arricao` = `arr`.`icao`
LEFT JOIN `aalv_test`.`airports` AS `dep` ON `schedules`.`depicao` = `dep`.`icao`
WHERE `pilots`.`id` = 419
However,
MYSQL returns error #1051 - Table airports does not exist.
I don't know what the issue is and Google hasn't helped. Any ideas? Also, if I only use one alias, I only get one airport but I need both. And the data is only in the table airports which according to this query, does not exist. Also, if I try throwing an AS section in the SELECT clause, I get error 1064: syntax error near AS.
EDIT: Database name is aalv_test, the .* at the end specifies to use all fields in the table, and the middle part is the table name, yes I am chaining fields.
Try this:
SELECT a.*, arr.*, dep.*, b.*
FROM bids AS b
LEFT JOIN aalv_test.pilots AS p ON b.pid = p.id
LEFT JOIN aalv_test.schedules AS s ON b.fid = s.id
LEFT JOIN aalv_test.aircraft AS a ON s.aircraft = a.id
LEFT JOIN aalv_test.airports AS arr ON s.arricao = arr.icao
LEFT JOIN aalv_test.airports AS dep ON s.depicao = dep.icao
WHERE p.id = 419;

Generate FULL JOIN with LinqToSQL

I have this linq query :
(from rapportBase in New_RapportReferencementBases
join rapportExtensionAll in New_RapportReferencementExtensionBases on rapportBase.New_RapportReferencementId equals rapportExtensionAll.New_RapportReferencementId into jointureRapportExtension
from rapportExtension in jointureRapportExtension.DefaultIfEmpty()
join packExtensionAll in New_PackExtensionBases on rapportExtension.New_PackId equals packExtensionAll.New_PackId into jointurePackExtension
from packExtension in jointurePackExtension.DefaultIfEmpty()
join packBaseAll in New_PackBases on packExtension.New_PackId equals packBaseAll.New_PackId into jointurePackBase
from packBase in jointurePackBase.DefaultIfEmpty()
join domaineBaseAll in New_DomaineBases on packExtension.New_DomaineId equals domaineBaseAll.New_DomaineId into jointureDomaineBase
from domaineBase in jointureDomaineBase.DefaultIfEmpty()
join domaineExtensionAll in New_DomaineExtensionBases on domaineBase.New_DomaineId equals domaineExtensionAll.New_DomaineId into jointureDomaineExtension
from domaineExtension in jointureDomaineExtension.DefaultIfEmpty()
join compteBaseAll in AccountBases on domaineExtension.New_AccountId equals compteBaseAll.AccountId into jointureCompteBase
from compteBase in jointureCompteBase.DefaultIfEmpty()
join compteExtensionAll in AccountExtensionBases on compteBase.AccountId equals compteExtensionAll.AccountId into jointureCompteExtension
from compteExtension in jointureCompteExtension.DefaultIfEmpty()
select rapportBase)
which generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
LEFT OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
LEFT OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
LEFT OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
LEFT OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
LEFT OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
LEFT OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
LEFT OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
But I want to generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
FULL OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
FULL OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
FULL OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
FULL OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
FULL OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
FULL OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
FULL OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
In other word, i want to generate full outer join for this query and not just left.
someone know how to do this in a simple way ?
thanks
There isn't a Full Outer Join in Linq. You have to do two left joins and concat them together. Here's some pseudocode that looks like linq:
var foj =
(from l in left
join r in right on l.Id equals r.Id into g
from r in g.DefaultIfEmpty()
select new { l, r })
.Concat
(from r in right
join l in left on r.Id equals l.Id into g
from l in g.DefaultIfEmpty()
where l == null
select new { l, r });
Probably be better to push this logic into a stored procedure if you're planning on using Linq to Sql.
See this post from the VB Team:
http://blogs.msdn.com/vbteam/archive/2008/02/12/converting-sql-to-linq-part-9-full-outer-join-bill-horst.aspx
Looks a little verbose.
Otherwise, there may be a way to achieve what you want using Foreign Keys and the Union operator.
Sadly I saw that before but i wanted to be sure.
The thing we did is to do it in a stored procedure and access it via linqtosql method.
then we map table result, like linqtosql do, using grouping to obtain collection of enties link to another entity.
for example, if we have an account with some contacts link to it. We request all contact with for each of them the account with a full join and them group on account id to obtain list of contact of each.
It work very well but it would be better if linqtosql was able to generate full join....