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
Related
I have successfully written this join query below to get all the data I need from each individual table in the database within one query. Unfortunately, it gives me ALL the data from these individual tables. I only need data that corresponds to e.Design_ID, if e.Design_ID = 1 I have tried a few "where e.Design_ID = 1" within this code block, but have not been successful in restricting the query. I'm stuck. I'm thinking I may need to restructure my entire query. My logic may be off...
Any help is much appreciated.
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
";
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
WHERE e.Design_ID = 1
";
It should work if you put it at the end. Also check correct spelling.
I've got a problem joining these tables; I have this code.
my query :
SELECT partial a.{ediTransactionDetailId, poNumber},
partial b.{edi997DetailId, noOfTrans},
partial c.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName},
partial d.{ediDocTypeId, docType}
FROM
MATRIXEDIBUNDLE:editransactiondetail a
JOIN a.edi997details b
JOIN b.editransaction c
JOIN c.edidoctype d
WHERE c.filename LIKE :fileName
AND a.ponumber LIKE :poNumber
AND d.doctype = :docType
AND a.flag = 1
AND c.flag = 1
and I got this error :
JOIN b.ediTransaction': Error: Class
Matrix\MatrixEdiBundle\Entity\EdiTransactionDetail has no association
named edi997Details
How can I join it?
You have not made relationship among tables, that's why you got the error here,
SELECT table1.column1, table2.column2...
FROM table1 JOIN table2
ON table1.common_field = table2.common_field; // (This part is missing in your code)
For further study http://www.tutorialspoint.com/sql/sql-using-joins.htm
Your code should look something like this,
SELECT a.edi_transaction_id, a.sender_id, a.receiver_id, a.gs_number, a.isa_number, a.file_name,
b.edi_997_detail_id, b.no_of_trans,
c.edi_transaction_etail_id, c.po_number,
d.edi_doc_type_id, d.doc_type
FROM (((edi_transaction a
left join edi_997_details b on a.edi_transaction_id = b.edi_transaction_id)
left join edi_transaction_details c on a.edi_transaction_id = c.edi_transaction_id)
left join edi_doc_type d on a.edi_doc_type_id = d.edi_doc_type_id)
WHERE a.file_name like '%Your file Name%'
and c.po_number like '%Your file Name%'
and d.doc_type = 'your doc type'
and a.flag = 1 AND c.flag = 1;
Hope this will accomplish your task.
Your need to learn JOIN sintaxis
either you need the ON clausule
FROM MatrixEdiBundle:EdiTransactionDetail a
JOIN Details b
ON a.SomeID = B.SomeID
Or you need a CROSS JOIN
FROM MatrixEdiBundle:EdiTransactionDetail a
CROSS JOIN Details b
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.
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;
Query Definition:
Select Students who have a grade of 85 or better in art
and
who also have a grade of 85 or better in any computer course
QUERY(explained in 3 sections is single query executed on MySQLWorkBench):
select Students.StudFirstName,Student_Schedules.Grade
from
(Select Distinct Students.StudentID,Students.StudFirstName,Student_Schedules.Grade
from (((Students
Inner Join Student_Schedules
On Student_Schedules.StudentID = Students.StudentID)
Inner Join Classes
On Classes.ClassID = Student_Schedules.ClassID)
Inner Join Subjects
On Subjects.SubjectID = Classes.SubjectID)
Inner Join Categories
On Subjects.CategoryID = Categories.CategoryID
where Categories.CategoryDescription = 'Art' and Student_Schedules.Grade >= 85)
As Stud_Art
Above code extracts Students who have a grade of 85 or better in Art
Inner Join
(Select Distinct Students.StudentID,Students.StudFirstName,Student_Schedules.Grade
from (((Students
Inner Join Student_Schedules
On Student_Schedules.StudentID = Students.StudentID)
Inner Join Classes
On Classes.ClassID = Student_Schedules.ClassID)
Inner Join Subjects
On Subjects.SubjectID = Classes.SubjectID)
Inner Join Categories
On Subjects.CategoryID = Categories.CategoryID
WHERE Categories.CategoryDescription LIKE '%Computer%' AND Student_Schedules.Grade >= 85)
As Stud_CS
Above code extracts Students who have a grade of 85 or better in Computer
On Stud_CS.StudentID = Stud_Art.StudentID;
Above code matches StudentID from Art and Computer
Error From MySQLWorkBench:
Error Code: 1054. Unknown column 'Students.StudFirstName' in 'field list'
The above Query may be solved by SubQuery technique but i want to learn how is it possible with Inner Join technique
You are trying to retrieve Students.StudFirstName and Student_Schedules.Grade but your table aliases are Stud_Art and Stud_CS. Might that be the issue?
I think that the first part should be like this:
select Stud_Art.StudFirstName,Stud_Art.Grade