MySQL warning of Unknown column that exists - php

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"

Related

Left Join with subquery returns Null

I am applying Left Join with Sub query & where clause
It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.
SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long
FROM activity_log_tim AS alt
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC
Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below
SELECT alt.userId, u.name,
t.name AS teamName,
alt.startDateTime,
v.name AS
villageName,
c.name AS clusterName,
startLat,
startLong,
COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default
COALESCE(latlng.long, 32)

MySQL join multiple tables error

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`

php select query unknown column in field list?

I'm having an error in mysql query (Error Code : 1054
Unknown column 'p.post_id' in 'field list')
post_id is present in the post table
can any one help me in this issue
SELECT u.iname , p.post_id,p.file_path
FROM users u
INNER JOIN likes l
ON u.user_id=l.user_id
INNER JOIN notify n
ON p.post_id=n.post_id
INNER JOIN post p
ON p.user_id=u.user_id
WHERE u.user_id=3 AND n.notify=1
Tables are not joined in the correct order, you cant reference post_id in the second join as the posts table isn't joined yet.
SELECT u.iname, p.post_id, p.file_path
FROM users u
INNER JOIN likes l ON u.user_id = l.user_id
INNER JOIN post p ON u.user_id p.user_id
INNER JOIN notify n ON p.post_id = n.post_id
WHERE u.user_id = 3 AND n.notify = 1

Unknown column 'reportes.id_usuario' in 'on clause'

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

Is it possible to perfom a fulltext search against a joined column?

Hello I' trying to select from joined column:
SELECT
A.idAd,
A.ads_in_Cat,
A.title,
A.currency,
A.price,
A.in_dpt,
A.description,
D.*,
S.*
FROM ads A
LEFT JOIN dept D ON D.id_dept = A.in_dpt
LEFT JOIN sub_cat_ad AS ON S.id_sub_cat = A.ads_in_Cat
WHERE MATCH(A.title, A.description) AGAINST(:searchTerm)
OR MATCH (S.sub_cat_name) AGAINST(:searchTerm)
Could someone please tell me why MySQL returns : 1191 Can't find FULLTEXT index matching the column list in... unless I use IN BOOLEAN MODE?
PS the table S has fulltext defined for each colunm. MySQL ver 5.1.49
Do you still get the error when you use:
SELECT A.idAd,
A.ads_in_Cat,
A.title,
A.currency,
A.price,
A.in_dpt,
A.description,
D.*,
s.*
FROM ads A
LEFT JOIN dept D ON D.id_dept = A.in_dpt
LEFT JOIN sub_cat_ad s ON s.id_sub_cat = A.ads_in_Cat
AND MATCH (s.sub_cat_name) AGAINST(:searchTerm)
WHERE MATCH(A.title, A.description) AGAINST(:searchTerm)

Categories