mySQL join same table twice - mysql

I'm trying this:
select
audit_log_entries.created_at,
audit_log_orig_term_types.name as originator,
audit_log_orig_term_types.name as terminator
from audit_log_entries
join audit_log_orig_term_types on audit_log_entries.originator_type_id = audit_log_orig_term_types.id
join audit_log_orig_term_types on audit_log_entries.terminator_type_id = audit_log_orig_term_types.id;
I think the intent is clear, I want both names for the originator and terminator. I have their IDs in the first table and the names on the other table.
I am getting an error from this: ERROR 1066 (42000): Not unique table/alias: 'audit_log_orig_term_types'
where's the mistake in the syntax?

You can make like this :
select
audit_log_entries.created_at,
audit1.name as originator,
audit2.name as terminator
from audit_log_entries
join audit_log_orig_term_types audit1 on audit_log_entries.originator_type_id = audit1.id
join audit_log_orig_term_types audit2 on audit_log_entries.terminator_type_id = audit2.id;

You need to alias the tables:
join audit_log_orig_term_types AS alias1 on audit_log_entries.originator_type_id = alias1.id
^^^^^^^^^ ^^^^^^
join audit_log_orig_term_types AS alias2 on audit_log_entries.terminator_type_id = alias2.id;

Use an alias for the joined tables:
Join table1 as t1 on t1.Id = [...]

Related

MySQL query: How to fix Error 1052 (ambiguous column)

I keep getting an error when I run both of the following queries that the CUST_NUM is ambiguous. How can I fix this?
SELECT INV_NUM, CUST_NUM, CUST_LNAME, CUST_FNAME, INV_DATE, INV_AMOUNT
FROM CH08_INVOICE i
INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM)
WHERE CUST_BALANCE>=1000;
SELECT CUST_LNAME, CUST_FNAME
FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2
ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
SELECT i.INV_NUM, i.CUST_NUM, i.CUST_LNAME, i.CUST_FNAME, i.INV_DATE, i.INV_AMOUNT FROM CH08_INVOICE i INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM) WHERE i.CUST_BALANCE>=1000;
SELECT c1.CUST_LNAME, c1CUST_FNAME FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2 ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
Please check this query
Ambiguous column means the database don't know which table it must use the column.
Try using
SELECT INV_NUM, i.CUST_NUM ...
or
SELECT INV_NUM, c1.CUST_NUM ...
for explicity defining the table.

Update Statement using Derived Table

I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id

MySQL JOIN Not unique table/alias:

SELECT punkty_skupu.id, punkty_skupu.nazwa_punktu, punkty_skupu.miejscowosc, trasy.czas_przejazdu_tekst
FROM punkty_skupu LEFT JOIN
punkty_skupu
ON trasy.id_punktu = punkty_skupu.id
Presumably, you are getting an error message because you have an undefined table alias:
SELECT punkty_skupu.id, punkty_skupu.nazwa_punktu, punkty_skupu.miejscowosc,
trasy.czas_przejazdu_tekst
FROM punkty_skupu LEFT JOIN
punkty_skupu trasy
------------------^
ON trasy.id_punktu = punkty_skupu.id

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;

How to Join 3 Tables in MySQL?

I am creating a view for my Database , I am joing 3 tables, Users,personal_info and contact_info, if you notice I have a lot of column names in my Select statement , since i don't want to include primary keys but it seems I have an error here, take a look
CREATE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
The error is
ERROR 1146: Table 'payroll.full' doesn't exist
SQL Statement:
CREATE OR REPLACE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
quote it with backtics: payroll.new_view
CREATE VIEW `payroll.new_view`
Error on:
LEFT JOIN personal_info on idUser = idPersonal_Info
you need to specify which column on which table equals which one on the other table, like
SELECT a,b,c from table1
LEFT JOIN table2
on table1.a= table2.columnY
in your case:
on USER.idUser = Personal_Info.idPersonalInfo
and the same for the 3rd Join
Another thing is the Comma at the end of the line:
LEFT JOIN personal_info on idUser = idPersonal_Info ,
it doesnt belong there.