I have the following MySQL query:
SELECT t.* , user_datos.user AS user_reportando, trans_datos.nombre AS trans_reportado
FROM reportes t
INNER JOIN user_trans ut ON( t.id_transporte=ut.id_transporte)
INNER JOIN user_datos ON (reportes.id_usuario = user_datos.user_id)
INNER JOIN trans_datos ON (reportes.id_transporte = trans_datos.trans_id)
WHERE ut.id_usuario='206'
ORDER BY fecha_reporte DESC
but its return
#1054 - Unknown column 'reportes.id_usuario' in 'on clause'
You are setting "t" as the alias for the reportes table. Try joining on the alias
SELECT
t.* ,
user_datos.user AS user_reportando,
trans_datos.nombre AS trans_reportado
FROM reportes t
INNER JOIN user_trans ut ON( t.id_transporte=ut.id_transporte)
INNER JOIN user_datos ON (t.id_usuario = user_datos.user_id)
INNER JOIN trans_datos ON (t.id_transporte = trans_datos.trans_id)
WHERE ut.id_usuario='206'
ORDER BY fecha_reporte DESC
Related
I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I cant figur out, why this statement does not run...
SELECT T1.* , T2.* , T3 . * , T4 . * , T5.Sessionname, T5.Session_ID, T5.Lernsession, T5.Session_Beschreibung, T5.User_ID, T6.Username, T6.ID, T7.*
FROM Session_Fragen T1, Sessions T5
INNER JOIN Fragen T4 ON T1.Frage_ID = T4.Frage_ID
INNER JOIN Semester T2 ON T2.Semester_ID = T4.Semester_ID
INNER JOIN Faecher T3 ON T3.Fach_ID = T4.Fach_ID
INNER JOIN Userdaten T6 ON T5.User_ID = T6.ID
LEFT JOIN Medien T7 ON T7.Frage_ID = T1.Frage_ID
WHERE
T1.Session_ID =1655
AND T1.User_ID =2
AND T5.Session_ID =1655
AND T4.Meldung =0
AND T4.Freigabe =1
AND T5.User_ID =2
ORDER BY T4.Fach_ID
i'm trying to solve the error: #1054 - Unknown column 'T1.Frage_ID' in 'on clause'
for couples of hours without any success...
may anybody sees what i'm doing wrong?
Please have a look at the query below - I am getting Unknown column 'u.id' in 'on clause'
SELECT id, username,
coalesce(
(SELECT name from company c
INNER JOIN user_company uc
ON uc.user_id = u.id
AND c.id = uc.company_id), 'NOT-AVAILABLE'
) companyname
FROM `user` u
Based on your comment, the correlation can't be placed within the JOIN of the correlated sub-query.
It can, however, be placed in the WHERE clause of the correlated sub-query.
SELECT
id,
username,
coalesce(
(SELECT name
FROM company c
INNER JOIN user_company uc
ON c.id = uc.company_id
WHERE uc.user_id = u.id
),
'NOT-AVAILABLE'
) companyname
FROM
`user` u
That answers your explicit question; why your query failed syntactically.
I would, however, replace the whole correlation with a simple LEFT JOIN.
SELECT
u.id,
u.username,
COALESCE(c.name, 'NOT-AVAILABLE') companyname
FROM
`user` u
LEFT JOIN
`user_company` uc
ON uc.user_id = u.id
LEFT JOIN
`company` c
ON c.id = uc.company_id
One way to fix it:
SELECT u.id,
u.username,
COALESCE(aux.name, 'NOT-AVAILABLE') as 'companyname'
FROM `user` u
LEFT JOIN
(SELECT user_id, name from company c
INNER JOIN user_company uc
ON c.id = uc.company_id) aux ON aux.user_id = u.id
Another way to fix it:
SELECT u.id,
u.username,
COALESCE(c.name, 'NOT-AVAILABLE') AS 'companyname'
FROM `user` u
LEFT JOIN user_company uc ON uc.user_id = u.id
LEFT JOIN company c ON c.id = uc.company_id
I have following MySQL Code:
SELECT *
FROM b_movies, b_mov_rol_celeb, b_starcast
LEFT JOIN bb_celebs ON b_mov_rol_celeb.celeb_id = bb_celebs.celeb_id
OR b_starcast.celeb_id = bb_celebs.celeb_id
LEFT JOIN bb_roles ON b_mov_rol_celeb.role_id = bb_roles.role_id
WHERE b_movies.id = '14';
Its giving error:
Unknown column 'b_mov_rol_celeb.celeb_id' in 'on clause'
Try this:
SELECT m.title_slug, m.title, m.release_date,
c.celeb_slug, c.celeb_name,
r.role_name,
s.char_name
FROM b_movies m
INNER JOIN b_starcast s ON m.id = s.movie_id
INNER JOIN b_mov_rol_celeb mrc ON m.id = mrc.movie_id
LEFT JOIN bb_celebs c ON mrc.celeb_id = c.celeb_id OR s.celeb_id = c.celeb_id
LEFT JOIN bb_roles r ON mrc.role_id = r.role_id
WHERE m.id = '14';
The order of the tables matters in a left or right join.
Having found out how to find PHP problems. I am now trying to solve them, but I have no clue on this one.
I made use of mysql_error and I have found:
1054: Unknown column 'o.user_id' in 'on clause'
Is there something wrong with this:
$sql="SELECT o.*, u.user_name, u.email, od.artist_id,cm.nexchange_price
FROM ".$tableprefix."orders o,".$tableprefix."currency_master cm
INNER JOIN ".$tableprefix."users u ON o.user_id = u.user_id
INNER JOIN ".$tableprefix."order_details od ON o.order_id = od.order_id
WHERE o.order_id = ".GetSQLValueString($orderid,"text")."
AND o.vorder_currency = cm.vcurrency_code ".$qryopt . " ORDER BY o.order_date DESC";
That column exists in the orders table?!
You have a comma after "orders o,", which means that you are trying to join the currency_master table with the users table, instead of orders and users. I suppose you wanted to have:
$sql="
SELECT
o.*, u.user_name, u.email, od.artist_id,cm.nexchange_price
FROM
".$tableprefix."currency_master cm,
".$tableprefix."orders o
INNER JOIN
".$tableprefix."users u
ON
o.user_id = u.user_id // et cetera"