MySQL JOIN Not unique table/alias: - mysql

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

Related

MySQL inner join w/ alias

So this is my table DENUNCIAS
id_denuncia,
id_categoria,
id_fecha,
email_denunciante -- FK table usuarios,
email_denunciado -- FK table usuarios,
descripcion,
fecha
As you can see i have a double reference to the same table.
What i'm trying to do is select (inner join) 'usuarios.nombreCompleto' twice, once for 'email_denunciante' and the other for 'email_denunciado' . This is where i am so far:
select
categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuarios.nombreCompleto as usuDenunciante,
usuarios.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias
inner join categorias on denuncias.id_categoria = categorias.id_categoria
inner join fechas on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante on denuncias.email_denunciante = usuarios.email
inner join usuarios as usuDenunciado on denuncias.email_denunciado = usuarios.email;
And this is the error i get:
Error Code: 1054. Unknown column 'usuarios.nombreCompleto' in 'field list'
I'd tried several methods with negative results.
Since it's a sintaxis error, not logic, i decided not to translate the code.
Thanks in advance!
You create table alias, you must use then on your select list. usuarios must be usuDenunciante or usuDenunciado.
Try this:
select categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuDenunciante.nombreCompleto as usuDenunciante,
usuDenunciado.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias
inner join categorias
on denuncias.id_categoria = categorias.id_categoria
inner join fechas
on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante
on denuncias.email_denunciante = usuDenunciante.email
inner join usuarios as usuDenunciado
on denuncias.email_denunciado = usuDenunciado.email;
I'm assuming that your usarious table really do contain the nombreCompleto column. If this is correct, I think the problem is, that you reference the usarious table directly instead of using the aliases you have made. Because you access that table twice, you need to specify which alias you mean to SELECT from.
Fx:
SELECT categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuDenunciante.nombreCompleto.as usuDenunciante,
usuDenunciado.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias inner join categorias on denuncias.id_categoria = categorias.id_categoria
inner join fechas on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante on denuncias.email_denunciante = usuarios.email
inner join usuarios as usuDenunciado on denuncias.email_denunciado = usuarios.email;

MySQL giving weird error

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'

mySQL join same table twice

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 = [...]

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;

Not unique table/alias:

Here's a query, i'm getting this error #1066 - Not unique table/alias: 'tbl_cp_list'
I have 2 database ie.
1) grameenphone_bill ---> 1 table ---> tbl_admin
2) android_appstore ---> 2 Tables ---> tbl_cp_list, tbl_list_data
SELECT `grameenphone_bill`.`tbl_admin`.`cp_id`,`grameenphone_bill`.`tbl_admin`.`cp_name`,`android_appstore`.`tbl_cp_list`.`cpid`,`android_appstore`.`tbl_cp_list`.`cpname`,`android_appstore`.`tbl_list_data`.`cp`,`android_appstore`.`tbl_list_data`.`Count`
FROM
`android_appstore`.`tbl_cp_list`
INNER JOIN `grameenphone_bill`.`tbl_admin`
ON `grameenphone_bill`.`tbl_admin`.`cp_id`=`android_appstore`.`tbl_cp_list`.`cpid`
INNER JOIN `android_appstore`.`tbl_cp_list`
ON `android_appstore`.`tbl_cp_list`.`cpname`=`android_appstore`.`tbl_list_data`.`cp`
Please tell , where i'm going wrong?
Thanks!!
You had return wrong table name in last JOIN.
Try this:
SELECT grameenphone_bill.tbl_admin.cp_id,grameenphone_bill.tbl_admin.cp_name,android_appstore.tbl_cp_list.cpid,
android_appstore.tbl_cp_list.cpname,android_appstore.tbl_list_data.cp,android_appstore.tbl_list_data.Count
FROM android_appstore.tbl_cp_list
INNER JOIN grameenphone_bill.tbl_admin ON grameenphone_bill.tbl_admin.cp_id=android_appstore.tbl_cp_list.cpid
INNER JOIN android_appstore.tbl_list_data ON android_appstore.tbl_cp_list.cpname=android_appstore.tbl_list_data.cp
You can also use alias names for tables
SELECT b.cp_id, b.cp_name, a.cpid, a.cpname, c.cp, c.Count
FROM android_appstore.tbl_cp_list a
INNER JOIN grameenphone_bill.tbl_admin b ON b.cp_id = a.cpid
INNER JOIN android_appstore.tbl_list_data c ON a.cpname = c.cp