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;
Related
I am having issues with getting this double left join to get the listingspecificsListPrice, but that info exists in the table, cant figure out why it would not include it. This is my sql.
SELECT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID AND mls_images.imgOrder = 0
WHERE userID = 413
GROUP BY mls_subject_property.mls_listingID
The result comes out like this..
All of the other fields come back, but it doesnt seem to want to bring back those two items.
This is a picture of the other table, to show that the data does in fact exist.
The mls_images.imgOrder = 0 condition should be in the join with mls_images, not mls_forms_listing_specifics.
Don't use GROUP BY if you're not using any aggregation functions. Use SELECT DISTINCT to prevent duplicates.
SELECT DISTINCT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID AND mls_images.imgOrder = 0
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID
WHERE userID = 413
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
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.
What is wrong in the following query:
SELECT * FROM forms
LEFT JOIN form_fields ON forms.id = form_field.parent_id
LEFT JOIN form_options ON form_field.id = form_options.parent_id
WHERE forms.name = activities
MySQL says 'unknown column 'activities'' where obviously forms.name should be seen as column name
I think you're just missing the quotes, try this:
SELECT * FROM forms
LEFT JOIN form_fields ON forms.id = form_field.parent_id
LEFT JOIN form_options ON form_field.id = form_options.parent_id
WHERE forms.name = 'activities'
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.