SQL : Unknown column in 'where clause' - php

I have a code :
$id=implode(",",$selected);
$query = "SELECT u.id, p.brand, n.number FROM `user` u
LEFT OUTER JOIN `phone` p ON u.id = p.id LEFT OUTER JOIN `number` n
ON p.id = n.id WHERE u.id in ($id)";
Where $selected is an array array(1,2,3). But when i run it, it appears this notice :
Unknown column '1' in 'where clause'
How to handle this problem? Thank you

Here you can do it like :
$id = implode("','",$selected);
This query will run :
$query = "SELECT u.id, p.brand, n.number FROM `user` u LEFT OUTER JOIN `phone` p ON u.id = p.id LEFT OUTER JOIN `number` n
ON p.id = n.id WHERE u.id in ('$id')";

Related

Add a WHERE clause to a MySQL query

I have this MySQL query that works really well. What it's doing is displaying all the filenames (filename) in a database, along with the poster of that filename and then any user who is tagged with that filename (cp.user_id). Later on in the script I explode the tagGroup.
I have a created a new field in the tbl_collab table called 'approved' which can either be 'yes' or 'no' and I want to know how to add a WHERE clause to my query so only the users where approved="yes" in tbl_collab are displayed. How can I do this? Many thanks.
"SELECT up.id, up.file, up.title, p.user_name, p.user_id, GROUP_CONCAT(CONCAT(cp.user_id, '~', cp.user_name) SEPARATOR '|') AS tagGroup
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file
LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid
GROUP BY up.file ORDER BY up.id DESC LIMIT ? , ?"
This is how I am exploding the tagGroup.
$tag_array = explode('|' , $tagGroup);
foreach($tag_array as $tag) {
list($uid,$uname) = explode('~',$tag,2);
You can add this condition in where clause like
"SELECT up.id, up.file, up.title, p.user_name, p.user_id, GROUP_CONCAT(CONCAT(cp.user_id, '~', cp.user_name) SEPARATOR '|') AS tagGroup
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file
LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid
WHERE c.approved = 'yes'
GROUP BY up.file ORDER BY up.id DESC LIMIT ? , ?"
Or you can add this condition in joining condition like
"SELECT up.id, up.file, up.title, p.user_name, p.user_id, GROUP_CONCAT(CONCAT(cp.user_id, '~', cp.user_name) SEPARATOR '|') AS tagGroup
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file AND c.approved = 'yes'
LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid
GROUP BY up.file ORDER BY up.id DESC LIMIT ? , ?"
Add WHERE c.approved = 'yes'to your sql query ;) or change the JOIN-condition to LEFT JOIN tbl_collab c ON (up.file = c.file AND c.approved = 'yes')

Adding a JOIN to query resulting in a fail

I am trying to add my users table to my existing query so that I can match the users.id with the forum_topics.topic_creator, so I can match those and enable myself to assign that with that users username.
This is my original query.
$query2 = mysqli_query($con,"SELECT t.*, COUNT(p.topic_id)
AS tid2 FROM forum_topics AS t JOIN forum_posts
AS p on t.id = p.topic_id WHERE t.category_id = ".$cid."
GROUP BY t.id DESC")
I then tried doing this..
$query2 =mysqli_query($con,"SELECT t.*, COUNT(p.topic_id)
AS tid2 FROM forum_topics AS t JOIN forum_posts
AS p on t.id = p.topic_id WHERE t.category_id = ".$cid."
INNER JOIN users AS u
ON t.topic_creator = u.id
GROUP BY t.id DESC")
or die ("Query2 failed: %s\n".($query2->error));
I am getting the fail message.
What am I doing wrong?
WHERE clause should come after join:
SELECT t.*, COUNT(p.topic_id) AS tid2
FROM forum_topics AS t JOIN
forum_posts AS p on t.id = p.topic_id INNER JOIN
users AS u ON t.topic_creator = u.id
WHERE t.category_id = ".$cid."
GROUP BY t.id DESC
EDIT:
To select the username as well:
SELECT t.*,u.username, COUNT(p.topic_id) AS tid2
FROM forum_topics AS t JOIN
forum_posts AS p on t.id = p.topic_id INNER JOIN
users AS u ON t.topic_creator = u.id
WHERE t.category_id = ".$cid."
GROUP BY t.id DESC
The WHERE clause has to be placed after the INNER JOIN.
$query2 =mysqli_query($con,"SELECT t.*, COUNT(p.topic_id)
AS tid2 FROM forum_topics AS t
JOIN forum_posts AS p ON t.id = p.topic_id
INNER JOIN users AS u ON t.topic_creator = u.id
WHERE t.category_id = ".$cid."
GROUP BY t.id DESC")
or die ("Query2 failed: %s\n".($query2->error));
Try this
SELECT t.*, COUNT(p.topic_id) AS tid2
FROM forum_topics t
JOIN forum_posts p ON (t.id = p.topic_id )
INNER JOIN users u ON (t.topic_creator = u.id)
WHERE t.category_id = ".$cid."
GROUP BY t.id DESC`

Mysql Query error in where clause on left join

Sorry for the basic question:
select `plans`.`name`, `A`.`subscription_id`, `A`.`amount`,
`A`.`created_date`, `A`.`end_date`,
`A`.`subscription_status`, `users`.`email`, `A`.`plan_id`,
`A`.`user_id`, `usage`.`created_at` as `usagedate`,
COUNT(usage.id) as used_count
from `subscriptions` A
left join `users` on `users`.`id` = `A`.`user_id`
left join `plans` on `A`.`plan_id` = `plans`.`Id`
left join `usage` on `A`.`user_id` = `usage`.`user_id`
where `usage`.`created_at` between A.created_at and A.end_date
group by `A`.`plan_id`
I am getting the error
1054 - Unknown column 'A.created_at' in 'where clause'
I think there should be A.created_date instead of A.created_at
select `plans`.`name`, `A`.`subscription_id`, `A`.`amount`,
`A`.`created_date`, `A`.`end_date`,
`A`.`subscription_status`, `users`.`email`, `A`.`plan_id`,
`A`.`user_id`, `usage`.`created_at` as `usagedate`, COUNT(usage.id) as
used_count from `subscriptions` A
left join `users` on `users`.`id` = `A`.`user_id`
left join `plans` on `A`.`plan_id` = `plans`.`Id`
left join `usage` on `A`.`user_id` = `usage`.`user_id`
where
`usage`.`created_at` between A.created_date and A.end_date
group by
`A`.`plan_id`
I would imagine it is meant to be a.created_date
SELECT `p`.`name`, `A`.`subscription_id`, `A`.`amount`, `A`.`created_date`, `A`.`end_date`, `A`.`subscription_status`, `u`.`email`, `A`.`plan_id`, `A`.`user_id`, `us`.`created_at` AS `usagedate`, COUNT(`us`.`id`) AS `used_count`
FROM `subscriptions` A
LEFT JOIN `users` u on `u`.`id` = `A`.`user_id`
LEFT JOIN `plans` p on `A`.`plan_id` = `p`.`Id`
LEFT JOIN `usage` us on `A`.`user_id` = `us`.`user_id`
WHERE `us`.`created_at` between `A`.`created_date` and `A`.`end_date`
GROUP BY `A`.`plan_id`

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

Table name alias scope in sub-select query

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

Categories